Solved

ex = {"'D:\\PipelineDashboardSource\\PipelineDashboard\\web' is not a valid virtual path."}

Posted on 2008-10-14
9
681 Views
Last Modified: 2012-05-05
i am not getting right path for web.config file.following exception is throwing
ex = {"'D:\\PipelineDashboardSource\\PipelineDashboard\\web' is not a valid virtual path."}

following code i am using to get webconfig path:

string configPath = System.Web.HttpContext.Current.Server.MapPath(@"/web");
0
Comment
Question by:ajay1feb
  • 2
  • 2
  • 2
  • +1
9 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22712127
Firstly, I think you want this:
string configPath = System.Web.HttpContext.Current.Server.MapPath("/web.config");

When you want to use this string which will then contain \, I would @ quote then but since you are using forward slash (relative path) I don't think that is necessary.
0
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22712236
Your code is exprecting a virtual directory path something like http://myserver/virtualdir/web.config
Server.MapPath method will give you the location of a file in the machine something like D:\myfolder\web.config

What you can do is, Get the URL of the page from current context and replace the file name with web.config if both page and web.config files are in same folder. Otherwise get the server name from current context and construct path of web.config


0
 

Author Comment

by:ajay1feb
ID: 22712567
when i tried following code it is still giving same error.

string configPath = System.Web.HttpContext.Current.Server.MapPath("/web.config");


// Get the Web application configuration object.
// i am getting error on following line
Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath);

0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 13

Expert Comment

by:TechTiger007
ID: 22712777
Its giving error because you are still using Server.MapPath which will give the location of the file in the hard disk where in the application is expecting virtual path as I explained before.

Try something like this

string configPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath.Replace("Home.aspx","web.config");// Server.MapPath(@"/web");
        Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(configPath);
0
 
LVL 22

Expert Comment

by:prairiedog
ID: 22713060
Try the attached code snippet..
For details:http://msdn.microsoft.com/en-us/library/ms151451(VS.80).aspx

WebConfigurationFileMap fileMap CreateFileMap();

System.Configuration.Configuration config = WebConfigurationManager.OpenMappedWebConfiguration(fileMap,"/config");

Open in new window

0
 

Author Comment

by:ajay1feb
ID: 22738804
dear prairiedog,

  when i am working with development server it is giving right path of web.config file but whenever i start working with iis then it is not getting web.config file which is on server, in this case it is taking machine.config file i belive.can you tell me what is code for getting web.config file on web server not on development server.

Regards,
ajay
0
 
LVL 22

Accepted Solution

by:
prairiedog earned 500 total points
ID: 22741879
Try this code snippet. It will write out the first connection string in your current web.config:
WebConfigurationFileMap fileMap = new WebConfigurationFileMap();

            string physDir = Server.MapPath(Request.ApplicationPath);
 

            VirtualDirectoryMapping vDirMapBase = new VirtualDirectoryMapping(physDir, true, "web.config");

            fileMap.VirtualDirectories.Add("/", vDirMapBase);

            

            System.Configuration.Configuration config = WebConfigurationManager.OpenMappedWebConfiguration(fileMap, "/config");

            Response.Write("Connection string: " + config.ConnectionStrings.ConnectionStrings[0]);

Open in new window

0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now