dotnet / dotnet/aspnetcore

Web.config transformations not working when deploying Blazor WebAssembly application to Production and Staging Environments

Open
#35,584 12 comments 1 reaction 0 assignees View on GitHub
area-blazor enhancement feature-blazor-deployment feature-blazor-wasm
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### 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](https://stackoverflow.com/questions/68852492/my-web-config-transformations-are-not-having-any-effect-when-i-publish-my-blazor) 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 here](https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/transform-webconfig?view=aspnetcore-5.0)

I want the transformation to add a custom header containing the blazor-environment variable [as documented here](https://docs.microsoft.com/en-us/aspnet/core/blazor/fundamentals/environments?view=aspnetcore-5.0#set-the-environment-via-header)

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-generated `web.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](https://github.com/BenjaminCharlton/WebConfigTransformations), or follow the steps below.
1) Create a new Blazor WebAssembly project.
2) Edit `Index.razor` so it looks like this:

```
@page "/"
@inject Microsoft.AspNetCore.Components.WebAssembly.Hosting.IWebAssemblyHostEnvironment Environment

Hello, world!

Environment: @Environment.Environment


```

3) In configuration manager:
i) Rename "Debug" to "Development"
ii) Rename "Release" to "Production"
ii) Add a new configuration called "Staging", based on "Production"
4) Right click on your project in Solution Explorer > Add new item > Add a `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.
5) Right click on your project and select "Publish".
6) Add a new FTP publish profile called "Staging" and save some details of an FTP server you can use as a staging environment. Specify the configuration "Staging" for this profile. Don't publish yet; just save the profile. Mine looks like this:

```


Staging
FTP
True
Release
Any CPU
temporaryurl1.myhost.net
False
12c14c2e-4d13-4e23-bf64-8e92faf909e9
ftp.myhost.net
True
True
myproject-stag
user
<_SavePWD>True
net5.0
false

```

7) Add a new FTP publish profile called "Production" and save some details of an FTP server you can use as a production environment. Specify the configuration "Production" for this profile. Don't publish yet; just save the profile. Mine looks like this:

```


Production
FTP
True
Release
Any CPU
temporaryurl2.myhost.net
False
12c14c2e-4d13-4e23-bf64-8e92faf909e9
ftp.myhost.net
True
True
myproject-stag
user
<_SavePWD>True
net5.0
false

```

8) Right click on each of the new `.pubxml` profiles in turn and choose "Add config transform". You should now have a `web.Staging.config` and a `web.Production.config` with some boilerplate code.
9) Paste this into `web.Staging.config`:

```







```

10) Paste this into `web.Production.config`:

```







```

11) Publish the Blazor application using each of the profiles "Production" and "Staging".

**What I hoped would happen**

- The home page of the application in the Production environment should say "Environment: Production" and the home page of the application in the Staging environment should say "Environment: Staging", while in my local development environment, it should still say "Environment: Development".
- The `web.config` file published at each site should have been transformed to contain the new `customHeader` with an appropriate value for `blazor-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 `` and ` ` and other nice stuff like that).
- The build/publish log in Visual Studio should (I guess) say something about transforming `web.config` (whether successful or failed).

**What actually happened**

- Neither of the published websites has a `web.config` at all, so running the site produces a 403 error.
- The build/publish log in VS says nothing about transforming `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 to `web.config` so the home page says "Environment: Production" in both Production and Staging environments.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.