Reputation: 649
I have a Blazor WebAssembly app which I want to automatically redirect to https if the user is attempting to access the web page via http.
For other non-WASM sites, I would do this within IIS Configuration, using "URL Rewrite" to redirect any such calls to the https equivalent. These rules are directly stored in a web.config file in the web folder.
However, when publishing a Blazor WebAssembly app to IIS, Visual Studio creates its own web.config file which contains other url rewrite rules required for the SPA to run/route correctly:
After publishing to IIS, I can go in and create my "http -> https" rule, which then gets added to the previously-generated web.config file.
The redirection from http to https then works a treat.
However, each time I publish the app to IIS, this web.config file will be overwritten, and my http to https rule lost.
Is there a way of defining this redirection rule somewhere within the Visual Studio Blazor WebAssembly project, such that it will automatically be included in the auto-generated web.config?
Any guidance would be greatly appreciated.
Upvotes: 3
Views: 3635
Reputation: 31
Create a new web.config in the root of the project directory:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<staticContent>
<remove fileExtension=".blat" />
<remove fileExtension=".dat" />
<remove fileExtension=".dll" />
<remove fileExtension=".json" />
<remove fileExtension=".wasm" />
<remove fileExtension=".woff" />
<remove fileExtension=".woff2" />
<mimeMap fileExtension=".blat" mimeType="application/octet-stream" />
<mimeMap fileExtension=".dll" mimeType="application/octet-stream" />
<mimeMap fileExtension=".dat" mimeType="application/octet-stream" />
<mimeMap fileExtension=".json" mimeType="application/json" />
<mimeMap fileExtension=".wasm" mimeType="application/wasm" />
<mimeMap fileExtension=".woff" mimeType="application/font-woff" />
<mimeMap fileExtension=".woff2" mimeType="application/font-woff" />
</staticContent>
<httpCompression>
<dynamicTypes>
<add mimeType="application/octet-stream" enabled="true" />
<add mimeType="application/wasm" enabled="true" />
</dynamicTypes>
</httpCompression>
<rewrite>
<rules>
<clear />
<rule name="Redirect to https" stopProcessing="true">
<match url=".*" />
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
</rule>
<rule name="Serve subdir">
<match url=".*" />
<action type="Rewrite" url="wwwroot\{R:0}" />
</rule>
<rule name="SPA fallback routing" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Rewrite" url="wwwroot\" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
and add the following lines to your project file:
<PropertyGroup>
<PublishIISAssets>true</PublishIISAssets>
</PropertyGroup>
Upvotes: 1
Reputation: 180948
<Target Name="CopyWebConfigOnPublish" AfterTargets="Publish">
<Copy SourceFiles="web.config" DestinationFolder="$(PublishDir)" />
</Target>
Should do the trick. Original source here.
Upvotes: 1