Visual Studio发布Blazor WASM到第三方主机时web.config转换失效怎么办
问题排查与解决方案
1. 转换文件结构错误
你当前的Web.Staging.config多套了一层<location>节点,Blazor WebAssembly应用自动生成的web.config中,system.webServer节点直接归属于根<configuration>节点,不存在外层<location>节点,路径不匹配导致转换规则完全无法命中。
修改后的转换文件内容如下:
<?xml version="1.0" encoding="utf-8"?> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <system.webServer xdt:Transform="InsertIfMissing"> <httpProtocol xdt:Transform="InsertIfMissing"> <customHeaders xdt:Transform="InsertIfMissing"> <add name="blazor-environment" value="Staging" xdt:Locator="Match(name)" xdt:Transform="InsertIfMissing" /> </customHeaders> </httpProtocol> </system.webServer> </configuration>
2. 检查发布配置文件属性
在Staging.pubxml的<PropertyGroup>节点中添加以下配置,显式开启web.config转换功能:
<IsTransformWebConfigDisabled>false</IsTransformWebConfigDisabled>
3. 检查转换文件的项目属性
在Visual Studio中右键Web.Staging.config,选择「属性」,确认两项配置:
- 生成操作:设置为
None - 复制到输出目录:设置为
不复制
4. 快速验证方法
修改配置后可以先选择发布到本地文件夹,直接查看输出的web.config是否已经插入预期配置,不需要每次发布到FTP验证,排查效率更高。
内容的提问来源于stack exchange,提问作者benjamin
相关产品推荐
相关产品推荐

