Solved

An error occurred loading a configuration file: Failed to map the path '/'.

Posted on 2010-09-06
23
3,265 Views
Last Modified: 2012-05-10
I recently upgraded from Windows Vista to Windows 7 and from IE 7 to IE 8

This is where the error occurs when trying to debug in local mode:

System.Configuration.Configuration rootWebConfig =
            System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/StockPro");

Any ideas?

Thanks,
0
Comment
Question by:Dovberman
  • 12
  • 11
23 Comments
 
LVL 2

Expert Comment

by:cyrusjoudieh
ID: 33609685
public static string GetPhysicalFilePath(string filePath)
{
filePath = filePath.Replace("/", "\\");
if (Path.EndsWith("\\") && filePath.StartsWith("\\"))
filePath = filePath.Substring(1);
return (Path + (Path.EndsWith("\\") ? String.Empty : "\\") + filePath + (filePath.Length > 0 ? (filePath.EndsWith("\\") ? String.Empty : "\\") : String.Empty));
}
System.Configuration.Configuration rootWebConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(App.GetPhysicalFilePath("StockPro"));
 
0
 
LVL 2

Expert Comment

by:cyrusjoudieh
ID: 33609705
continue...
forgot to include the Path variable.
public static string Path = HttpContext.Current.Request.PhysicalApplicationPath;
0
 

Author Comment

by:Dovberman
ID: 33611711
What does this mean?

System.Configuration.Configuration rootWebConfig =
            System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(App.GetPhysicalFilePath("StockPro"));

Error      1      The name 'App' does not exist in the current context      C:\Develop\Apps\StockPro\Default.aspx.cs      48      83      C:\...\StockPro\

           
0
 

Author Comment

by:Dovberman
ID: 33611832
Perhaps more of the code would be helpful:

public partial class _Default : System.Web.UI.Page
{
    public System.Configuration.ConnectionStringSettings connString;
    public System.Configuration.ConnectionStringSettings connStringMember;

    public int intDays = 0;
    public string strUserName = "";
    public MembershipUser currentUser;
 
    public string strConnection;
    public string strConnectionMember;
    public string strUserID = "";

    public static string GetPhysicalFilePath(string filePath)
    {
        filePath = filePath.Replace("/", "\\");
        if (Path.EndsWith("\\") && filePath.StartsWith("\\"))
            filePath = filePath.Substring(1);
        return (Path + (Path.EndsWith("\\") ? String.Empty : "\\") + filePath + (filePath.Length > 0 ? (filePath.EndsWith("\\") ? String.Empty : "\\") : String.Empty));
    }

    public static string Path = HttpContext.Current.Request.PhysicalApplicationPath;

   
    protected void Page_Load(object sender, EventArgs e)
    {
        //System.Configuration.Configuration rootWebConfig =
        //    System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/StockPro");

        System.Configuration.Configuration rootWebConfig =
            System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(App.GetPhysicalFilePath("StockPro"));
           

0
 
LVL 2

Expert Comment

by:cyrusjoudieh
ID: 33612961
try using this:

System.Configuration.Configuration rootWebConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null, null, "StockPro");
0
 

Author Comment

by:Dovberman
ID: 33613024
This gives me null connection strings.

my app worked perfectly until I upgraded from Windows Vista to Windows 7, and from IE 7 to IE 8.
0
 
LVL 2

Expert Comment

by:cyrusjoudieh
ID: 33613033
Usually I use this method for reading the configuration.
WebConfigSettingsEnum : I use it to define the key names. For ease of access in function.

<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="EnableDoubleWindowCheck" value="0"></add>
<add key="AutoLoginRememberMe" value="1"></add>
..............
Just call it like:
string Value = App.WebConfig.ReadKey(App.WebConfig.WebConfigSettingsEnum.AutoLoginRememberMe);


sample code:

public static bool AutoLoginRemeberMe
{
get
{
return App.WebConfig.ReadKey(App.WebConfig.WebConfigSettingsEnum.AutoLoginRememberMe) == "1";
}
}

public static class WebConfig
{
public enum WebConfigSettingsEnum
{
Redirect = 1,
EnableErrorLog = 2,
EnableDoubleWindowCheck = 3,
AutoLoginRememberMe = 4
}
public static string ReadKey(WebConfigSettingsEnum Enum)
{
string Value = ConfigurationManager.AppSettings[Enum.ToString()];
return Value != null ? Value : String.Empty;
}
}
 _______________________________________________________________
Regarding the App class, you can ignore/remove the "App" which referes to a static class which I missed to post it earlier.
If you insist calling it through the App class (App.GetPhysicalFilePath("Folder"))  then you need to define it as static App class like in the code below:
public partial class Default : System.Web.UI.Page
{

protected void Page_Init(object sender, EventArgs e)
{
System.Configuration.Configuration rootWebConfig  =  System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(App.GetPhysicalFilePath("StockPro"));
}

public static class App
{
     public static string Path = HttpContext.Current.Request.PhysicalApplicationPath;
      public static string GetPhysicalFilePath(string filePath)
      {
            filePath = filePath.Replace("/", "\\");
            if (Path.EndsWith("\\") && filePath.StartsWith("\\"))
            filePath = filePath.Substring(1);
            return (Path + (Path.EndsWith("\\") ? String.Empty : "\\") + filePath + (filePath.Length > 0 ? (filePath.EndsWith("\\") ? String.Empty : "\\") : String.Empty));
      }
}

}
0
 
LVL 2

Expert Comment

by:cyrusjoudieh
ID: 33613053
if your webconfig is located in the root folder only of the application then you only need to do is to send a null parameter.
which will be:

System.Configuration.Configuration rootWebConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);
 
0
 

Author Comment

by:Dovberman
ID: 33613570
This is more complicated than I thought. I will try to figure it out.

The app worked before I upgraded.
0
 
LVL 2

Expert Comment

by:cyrusjoudieh
ID: 33613587
it will work, there is no special case about the operating system. Could be the framwork version changed since you upgraded.
define the connection string under AppSettings as
<add key="Dsn" value="data source=127.0.0.1;initial catalog=database;user id=username;password=password"/>
to read the key:

string DSN = ConfigurationManager.AppSettings["Dsn"];
you can trap the case if the key is null like the function above "ReadKey"
 
0
 

Author Comment

by:Dovberman
ID: 33613674
Thanks,

I willl spend time with this tomorrow.

David
0
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.

 

Author Comment

by:Dovberman
ID: 33617136
The app works fine remotely. It fails only when trying to open locally. I need to look at my IIS installation and Cassini.
0
 
LVL 2

Expert Comment

by:cyrusjoudieh
ID: 33617227
Actually if the folder resides outside the wwwroot folder make sure that the user IUSER_xxxxx has access identical to the ones in wwwroot .
It depends how your application is set, Virtual folder or as a new website.
Its better to have it standalone as a new website you can change the bindings of each website you have.
Web.config should exist only in the root directory of the website, if you are having a virtual folder you can't have the web.config working there.
example:
Default Webiste: http://localhost/ point to inetpub/wwwrooot

either publish your application under wwwroot or create another folder
under Intepub which will look like this scheme
Inetpub
--MyNewWebsite
--wwwroot

1- Create the folder
2- Add the IUSER_XXXX on MyNewWebsite folder.
3- Create the new website in the IIS and point it to the inetpub/MyNewWebsite
4- Configure the binding which should listen on port number, can be 82 it will be like http://localhost:82/
5- Publish your application into the new folder MyNewWebsite (If you faced problems publishing into your new folder make sure the user owner has full access)
Access your application by browsing into http://localhost:XX/
if you set the binding to 80 then there is no need to specify the port number after localhost
0
 

Author Comment

by:Dovberman
ID: 33617974
Problem solved. VS should be set to run as administrator.

http://cs.rthand.com/blogs/blog_with_righthand/archive/2007/12/01/WebConfigurationManager.OpenWebConfiguration-throws-Failed-to-map-the-path-_27002F0027002E00_.aspx

Right click the VS desktop icon.
Select the Compatibility Tab.
In the Privilege Level section, Check "Run this program as administrator".

Thanks for all your effort.
0
 
LVL 2

Expert Comment

by:cyrusjoudieh
ID: 33619416
this is a work around and not a solution, it means you don't have the correct permissions in your folders of your IIS , you should run VS in Normal Mode without the Admin Rights.
0
 
LVL 2

Expert Comment

by:cyrusjoudieh
ID: 33619565
Visual Studio has its own WebService for debugging.
Visual Studio Projects should be in normal folders with current user rights. Check your folders and inherit them to subfolders as shown in the pictures attached (3 pictures).

You can see the IIS_User permissions of the folder where the project is published to, this is a standalone website. (1 picture)

AP.PNG
AP2.PNG
AP3.PNG
IIS.PNG
0
 

Author Comment

by:Dovberman
ID: 33619790
Please reopen and give all points to cyrusjoudieh.

Thanks,

Dovberman
0
 
LVL 2

Accepted Solution

by:
cyrusjoudieh earned 500 total points
ID: 33620938
I found the solution that will make it run without the need of Administrator privilege.
  1. Right Click on the Solution and choose Properties Window
  2. In the Properties Window change the Virtual Path in to / as show in the picture attached
  3. Run the project it should be something like http://localhost:port/
    it should work
Paste the attached code into an ASPX file and execute it, you should get the connection strings in the page if set in web.config.
Good Luck!

<%@ Page Language="C#" AutoEventWireup="true" %>





<script type="text/C#"  runat="server">



    protected void Page_Init(object sender, EventArgs e)

    {



        System.Configuration.Configuration rootWebConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/StockPro");



        System.Configuration.ConnectionStringSettings connString;

        if (rootWebConfig.ConnectionStrings.ConnectionStrings.Count > 0)

        {

            connString = rootWebConfig.ConnectionStrings.ConnectionStrings["NorthwindConnectionString"];

            if (connString != null)

                Response.Write("Northwind connection string = \"" + connString.ConnectionString + "\"");

            else

                Response.Write("No Northwind connection string");

        }

    }

</script>

Open in new window

Prop.PNG
VP.PNG
0
 

Author Comment

by:Dovberman
ID: 33623124
Please reopen. New solutions that work have been presented.
0
 

Author Comment

by:Dovberman
ID: 33625958
I do not have the Property option.

I am the only user of the development computer. therefore I can always run as administrator.

Thanks,


Property-Menu.jpg
0
 

Author Comment

by:Dovberman
ID: 33625977
I will leave the app as is. The connections are recognized.


 protected void Page_Load(object sender, EventArgs e)

    {

        System.Configuration.Configuration rootWebConfig =

            System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/StockPro");



        if (0 < rootWebConfig.ConnectionStrings.ConnectionStrings.Count)

        {

            connString =

                rootWebConfig.ConnectionStrings.ConnectionStrings["ConStockSelect"];

            connStringMember= 

                rootWebConfig.ConnectionStrings.ConnectionStrings["MemberShipSQLConnectionString"];

            

            if (null != connString)

                Console.WriteLine("StockPro connection string = \"{0}\"",

      

              connString.ConnectionString);

            else

                Console.WriteLine("No StockPro connection string");

        }



        // Define the ADO.NET Connection object.

        string strConnection = connString.ToString();

        SqlConnection conStockSelect = new SqlConnection(strConnection);

        conStockSelect.Open();



        string strIPAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];

        

        string  strUserName = User.Identity.Name;

        MembershipUser currentUser = Membership.GetUser(strUserName);

        

    

        if (User.Identity.IsAuthenticated)

        {

            lblWelcome.Text = "Welcome " + strUserName;

            

            

            // Get subscription renewal date

            string strUserID = currentUser.ProviderUserKey.ToString();

            string strRenewSQL = "SELECT TOP 1 UserID,DATEADD(m, 1, SubscriptionDate) AS RenewDate  ";

            strRenewSQL += ", SubscriptionDate FROM Subscriptions ";

            strRenewSQL += "WHERE UserID = '" + strUserID + "' ";

            strRenewSQL += "ORDER BY SubscriptionDate DESC";



            string strRenewDate = GetColValue(conStockSelect, strRenewSQL, "RenewDate");

            if (strRenewDate == "None")

            {

                // Not a subscriber

                lblExpired.Text = strUserName + " does not subscribe";

                return;

            }

Open in new window

0
 

Author Closing Comment

by:Dovberman
ID: 33625982
Thank you.
0
 
LVL 2

Expert Comment

by:cyrusjoudieh
ID: 33628457
Yes, it seems you have a web application and not a webiste solution.
To acheive this, you need to right click on the web application and choose properties then navigate to the third tab on your left "web" and check the virtual path to be /.
Also attached the page after the "Start Debugging" was executed.
VS was running without Admin privileges.

 
You are welcome!

WebApplication.PNG
Result.PNG
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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.…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

10 Experts available now in Live!

Get 1:1 Help Now