【问题标题】:Relative virtual path is not allowed when using server.MapPath()使用 server.MapPath() 时不允许使用相对虚拟路径
【发布时间】:2016-01-05 15:38:34
【问题描述】:

我正在尝试使用以下代码获取配置文件:

public void EncryptConnString()
        {
    Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Server.MapPath(@"/"));
            ConfigurationSection section = config.GetSection("connectionStrings");
            if (!section.SectionInformation.IsProtected)
            {               
                config.Save(ConfigurationSaveMode.Modified);
            }
        }

但我得到了错误

此处不允许使用相对虚拟路径“F:/xxxx/yyyy/sample/”。

注意:我在global.asax 页面访问此代码我做错了什么?

【问题讨论】:

  • HttpContext.Current.Server.MapPath("~/")
  • 对不起,遇到同样的错误!
  • 注意:我在 global.asax 中访问此代码,如果我在页面方法中调用相同的方法意味着它的工作正常
  • 更新了页面方法

标签: c# asp.net configuration web-config global-asax


【解决方案1】:

如果您将 null 传递给此方法,它将为您返回根配置文件:

var config = WebConfigurationManager.OpenWebConfiguration(null);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-07
    • 2011-06-29
    • 1970-01-01
    相关资源
    最近更新 更多