繁体   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