New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Web.config transformations not working when deploying Blazor WebAssembly application to Production and Staging Environments #35584
Comments
Thanks for your feedback. Blazor WebAssembly currently does not support web.config transform. We'll consider this an enhancement for the backlog. For the time being, we would recommend using a custom web.config. The default contents for the web.config that Blazor adds (using the output you get from publishing the app). |
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
Were you ever able to get a web.config that allows you to still run locally? |
For a stand-alone Blazor WASM project, you should be able to do this at build time (rather than at publish time) if you use the SlowCheetah library as described on the accepted answer on my StackOverflow question. But, if your project uses Blazor WASM with pre-compilation on the server (as described here), as mine now does, then I still haven't yet found the solution, and would love to know. |
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
As a workaround for adding the <Project>
<UsingTask
TaskName="AddBlazorEnvironment"
TaskFactory="RoslynCodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >
<ParameterGroup>
<Template ParameterType="System.String" Required="true" />
<Target ParameterType="System.String" Required="true" />
<Environment ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Reference Include="$(OutputPath)System.Xml.XDocument.dll" />
<Using Namespace="System.Xml.Linq" />
<Code Type="Fragment" Language="CS">
<![CDATA[
XElement GetOrAdd(XElement elem, string name) {
var child = elem.Element(name);
if (child == null) {
elem.Add(child = new XElement(name));
}
return child;
}
var doc = XDocument.Load(Template);
var elem = doc.Root;
elem = GetOrAdd(elem, "system.webServer");
elem = GetOrAdd(elem, "httpProtocol");
elem = GetOrAdd(elem, "customHeaders");
const string HEADER_NAME = "blazor-environment";
var add = elem
.Elements("add")
.FirstOrDefault(e => e.Attribute("name")?.Value == HEADER_NAME);
if (add == null) {
elem.Add(add = new XElement("add",
new XAttribute("name", HEADER_NAME)
));
}
add.SetAttributeValue("value", Environment);
doc.Save(Target);
]]>
</Code>
</Task>
</UsingTask>
<Target Name="TrasformBlazorWebConfigFile"
AfterTargets="_AddBlazorWebConfigFile">
<ItemGroup>
<BlazorTemplateWebConfig Include="@(ResolvedFileToPublish)" Condition=" '%(Filename)%(Extension)' == 'BlazorWasm.web.config' " />
<BlazorTransformedWebConfig Include="@(BlazorTemplateWebConfig->'$(IntermediateOutputPath)web.config')" />
</ItemGroup>
<AddBlazorEnvironment
Condition="'@(BlazorTemplateWebConfig)' != ''"
Template="@(BlazorTemplateWebConfig)"
Target="@(BlazorTransformedWebConfig)"
Environment="$(EnvironmentName)"
/>
<ItemGroup Condition="'@(BlazorTemplateWebConfig)' != ''">
<ResolvedFileToPublish Remove="@(BlazorTemplateWebConfig)" />
<ResolvedFileToPublish
Include="@(BlazorTransformedWebConfig)"
ExcludeFromSingleFile="true"
CopyToPublishDirectory="PreserveNewest"
RelativePath="web.config" />
</ItemGroup>
</Target>
</Project> It assumes you don't have an existing |
Holy moley, Markus, this looks perfect! Thank you very much for thinking of me! |
@MizardX I tried this solution but not sure how to inject that |
You can add the parameter |
There should be some kind of publish error if this is not supported. Huge time waster. |
It is unfortunate that it is not compatible, year 2024 and it still does not work.... |
Describe the bug
I'm not 100% sure if this is a problem with Visual Studio's publishing wizard, or a problem with Blazor, or just something that I did wrong.
I asked on StackOverflow and learned some useful things from the community but we have not solved the problem so far.
Summary
I want to apply a
web.config
transformation when publishing my Blazor WebAssembly application to my Staging and Production environments via FTP using the Visual Studio Publish Wizard, as described hereI want the transformation to add a custom header containing the blazor-environment variable as documented here
Naturally, I don't want the addition of a
web.config
file to break my local development environment either (I've noticed that's exactly what happens if you just paste the auto-generatedweb.config
from the production/staging environment into the project root).No matter what I try, my
web.config
transformations don't get applied when I publish the site, and publishing takes an hour each time, so progress is slow!To Reproduce
You can view a minimal reproducible sample here, or follow the steps below.
Index.razor
so it looks like this:i) Rename "Debug" to "Development"
ii) Rename "Release" to "Production"
ii) Add a new configuration called "Staging", based on "Production"
web.config
file. The default one is mostly blank, except for the XML declaration, a comment, a pair of configuration tags. Leave it alone because, in my experience, adding anything to it breaks the project when debugging locally. Check that your project still builds and runs locally. It should..pubxml
profiles in turn and choose "Add config transform". You should now have aweb.Staging.config
and aweb.Production.config
with some boilerplate code.web.Staging.config
:web.Production.config
:What I hoped would happen
web.config
file published at each site should have been transformed to contain the newcustomHeader
with an appropriate value forblazor-environment
. I would expect other transformations to have been applied by the publishing wizard to make Blazor run on IIS (usually, for example, it adds<staticContent><mimeMap>
and<httpCompression><dynamicTypes>
and other nice stuff like that).web.config
(whether successful or failed).What actually happened
web.config
at all, so running the site produces a 403 error.web.config
.Something else I tried that didn't work
I have also tried the above steps but with a complete
web.config
file in the project root (I got the content off a Blazor WASM application that was published to IIS. This prevents the project running at all in the development environment but it does at least run in Production and Staging. Still, though, no transformations got applied toweb.config
so the home page says "Environment: Production" in both Production and Staging environments.The text was updated successfully, but these errors were encountered: