How to replace OpenExeConfiguration in a web context (asp.net mvc 1)

后端 未结 1 1735
情话喂你
情话喂你 2021-01-07 19:26

OK so we have something that is currently using OpenExeConfiguration for reading a config file, however this doesn\'t work when running in the web context.

I\'ve tri

相关标签:
1条回答
  • 2021-01-07 19:40

    So in the end I used this code (had to handle whether the web application was running, or if our unit test code was running).

    System.Configuration.Configuration config = null;
    
    if (System.Web.HttpContext.Current != null && !System.Web.HttpContext.Current.Request.PhysicalPath.Equals(string.Empty))
            config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
    else
            config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    

    Also have to be running Visual Studio in Administrator mode - which I found out you can set as a property on your shortcut so you don't need to remember each time in Windows 7 to right click and run as administrator :)

    0 讨论(0)
提交回复
热议问题