我想使用Web.config转换,该转换适用于发布也适用于调试.
当我发布Web应用程序时,Visual Studio会根据我的currenctbuild配置自动转换Web.config.当我开始调试时,如何告诉Visual Studio执行相同的操作?在调试启动时,它只使用默认的Web.config而不进行转换.
任何的想法?
我有两个目标构建环境; 发布和分期.Web.config看起来像这样:
<system.web>
<authentication mode="Windows">
</authentication>
<authorization>
<deny users="?" />
</authorization>
</system.web>
Run Code Online (Sandbox Code Playgroud)
我想通过构建到暂存配置来转换它:Web.Staging.config
<system.web>
<authentication mode="Windows">
</authentication>
<authorization xdt:Transform="Replace">
<deny users="?" />
<allow roles="StagingRoles" />
<deny users="*" />
</authorization>
</system.web>
Run Code Online (Sandbox Code Playgroud)
我从命令行构建如下:
msbuild buildscript.build /p:Configuration=Staging
Run Code Online (Sandbox Code Playgroud)
在构建之后,我没有看到在构建工件文件夹中转换的web.config文件.这里有什么问题吗?
谢谢
我有一个ASP.NET Web应用程序项目,通过实体框架连接到远程数据库.在调试期间(例如,在我的本地计算机上运行项目),数据库的IP地址与发布期间的IP地址不同(例如,在将项目上载到我的Web服务器并从浏览器运行之后).到目前为止,我总是手动更改Web.config文件中的数据库连接字符串以在两者之间切换(基本上我必须连接字符串,一个名为'Debug',一个'Release',我只是在部署时交换名称).
现在我只是注意到应该可以通过Web.config Transformation Syntax自动发生这种情况,你可以将修改后的连接字符串放在Web.Release.config版本中,然后在Release版本下构建DLL时应该使用它.
但它似乎对我不起作用......
这是我的常规Web.config文件的相关部分(其中包含用于本地使用的Debug连接字符串):
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<!-- Debug connection string. Release connection string is in Web.Release.config file -->
<add name="DatabaseEntities" connectionString="A" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
这是Web.Release.config文件,如果DLL处于Release模式下,根据示例应将"DatabaseEntities"连接字符串"A"替换为"B":
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!-- Replace the DatabaseEntities connection string with the Release version (local IP address) -->
<connectionStrings>
<add name="DatabaseEntities"
connectionString="B"
xdt:Transform="Replace" xdt:Locator="Match(name)"/>
</connectionStrings>
</configuration>
Run Code Online (Sandbox Code Playgroud)
(显然"A"和"B"只是我真实连接字符串的占位符)
当我调试应用程序时(例如只按F5),使用默认的Web.config,我可以访问数据库.然后,我通过Configuration Manager将构建配置更改为Release.解决方案中的所有项目都设置为发布配置.然后我构建解决方案(仅通过Build或甚至通过完整的重建(例如Clean,Rebuild)).我将新构建的DLL上传到Web服务器,以及Web.config和Web.Release.config文件,当我尝试访问我无法访问的数据库时,它仍然试图通过调试IP地址访问数据库因此无法找到它......
似乎Web.Release.config文件被完全忽略,或者至少没有替换连接字符串.
我究竟做错了什么?转换语法错了吗?我没有正确地在发布模式下构建应用程序吗?
我的调试和发布web.config应用程序设置未正确读取.
Web.config文件:
<appSettings>
<add key="webpages:Version" value="1.0.0.0"/>
<add key="ClientValidationEnabled" value="true"/>
<add key="UnobtrusiveJavaScriptEnabled" value="true"/>
</appSettings>
Run Code Online (Sandbox Code Playgroud)
Web.Debug.config
<appSettings>
<add key="ErrorEmailAddress" value="developerEmail@email.com" />
<add key="TestModeEmailAddress" value="developerEmail@email.com" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)
Web.Release.config
<appSettings>
<add key="ErrorEmailAddress" value="prodErrorEmail@email.com" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)
但是,致电:
WebConfigurationManager.AppSettings["ErrorEmailAddress"]
Run Code Online (Sandbox Code Playgroud)
返回null(调试时).
我试过添加xdt:Transform ="Insert"例如
<add key="ErrorEmailAddress" value="prodErroEmail@email.com" xdt:Transform="Insert" />
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我想在项目生产(发布模式)时显示自定义错误页面,并在(调试模式)运行页面时显示asp.net的默认错误页面.
我的项目中有三个Web.config文件.
这是我尝试但没有运气.
Web.config文件
<customErrors mode="On" defaultRedirect="~/Home/Error">
<error statusCode="404" redirect="~/Home/Error" />
</customErrors>
Run Code Online (Sandbox Code Playgroud)
Web.Debug.config
<customErrors mode="Off" xdt:Transform="Replace"></customErrors>
Run Code Online (Sandbox Code Playgroud)
Web.Release.config
<customErrors mode="On" defaultRedirect="~/Home/Error">
<error statusCode="404" redirect="~/Home/Error" />
</customErrors>
Run Code Online (Sandbox Code Playgroud)
有谁知道我怎么做到这一点?
提前致谢 :)
编辑: 阅读后,我认为这是一个已知的错误,但我无法找到错误的修复程序.如果有人对此有更多了解,请分享.谢谢