簡體   English   中英

Blazor WebAssembly - 如何在自動生成的 web.config 中添加“http 到 https”URL 重寫規則

[英]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.

對於其他非 WASM 站點,我將在 IIS 配置中執行此操作,使用“URL 重寫”將任何此類調用重定向到 https 等效項。 這些規則直接存儲在 web 文件夾中的 web.config 文件中。

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 文件的內容

發布到 IIS 后,我可以在 go 中創建我的“http -> https”規則,然后將其添加到之前生成的 web.config 文件中。

添加我的新重定向規則

從 http 到 https 的重定向然后會起作用。

However, each time I publish the app to IIS, this web.config file will be overwritten, and my http to https rule lost.

有沒有辦法在 Visual Studio Blazor WebAssembly 項目中的某處定義此重定向規則,以便它自動包含在自動生成的 web.config 中?

任何指導將不勝感激。

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

應該做的伎倆。 原始來源在這里

在項目目錄根目錄下新建web.config:

<?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>

並將以下幾行添加到您的項目文件中:

    <PropertyGroup>
        <PublishIISAssets>true</PublishIISAssets>
    </PropertyGroup>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM