简体   繁体   中英

Blazor WebAssembly - how to add a "http to https" URL rewrite rule in auto-generated web.config

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.

IIS - URL 重写

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:

自动生成的 web.config 文件

自动生成的 web.config 文件的内容

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.

<Target Name="CopyWebConfigOnPublish" AfterTargets="Publish">
  <Copy SourceFiles="web.config" DestinationFolder="$(PublishDir)" />
</Target>

Should do the trick. Original source here .

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM