Azure Web 应用程序忽略 web.config URL 重写

问题描述 投票:0回答:1

我有一个天蓝色的网络应用程序。它被部署为一个针对 net8.0 的框架依赖应用程序,并且运行时目标是可移植的。

我写了一个web.config,位于wwwroot目录下。

我正在尝试使用 URL 重写将默认域 sitename.azurewebsites.net 重定向到我的实际网站 https://sitename.com 我不希望默认站点可访问。

无论我尝试什么,网络应用程序似乎都会忽略我的 web.config。 web.config内容如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <location path="." inheritInChildApplications="false">
        <system.webServer>
            <rewrite>
                <rules>
                    <rule name="Redirect rquests to default azure websites domain" stopProcessing="true">
                        <match url="(.*)" />
                        <conditions>
                            <add input="{HTTP_HOST}" pattern="^sitename\.azurewebsites\.net$" />
                        </conditions>
                        <action type="Redirect" url="https://sitename.com.au/{R:0}" redirectType="Permanent" />
                    </rule>
                </rules>
            </rewrite>
            <handlers>
                <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
            </handlers>
            <aspNetCore processPath="dotnet" arguments=".\sitename.dll" stdoutLogEnabled="false" stdoutLogFile="\\?\%home%\LogFiles\stdout" hostingModel="inprocess" />
        </system.webServer>
    </location>
</configuration>

任何人都可以指出我做错了什么吗?

我尝试更改将 web.config 发布到服务器的方式,我可以确认它位于站点根目录中。 Azure 只是忽略它。

url-rewriting azure-web-app-service web-config
1个回答
0
投票

无论我尝试什么,网络应用程序似乎都会忽略我的 web.config

它会被忽略,因为我们需要将 web.config 文件放在项目的根目录中,而不是将其保留在 wwwroot 文件夹中。 当我们发布 ASP.NET Core 应用程序时,它会默认在 KUDU 的 home/site/wwwroots 中创建一个 web.config 文件。

enter image description here

在下面的代码中,我使用 https://www.google.com 作为重定向 URL。

web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <location path="." inheritInChildApplications="false">
        <system.webServer>
            <rewrite>
                <rules>
                    <rule name="Redirect requests to default Azure Websites domain" stopProcessing="true">
                        <match url="(.*)" />
                        <conditions>
                            <add input="{HTTP_HOST}" pattern="^urlrewritingapp2024\.azurewebsites\.net$" />
                        </conditions>
                        <action type="Redirect" url="https://www.google.com/" redirectType="Permanent" />
                    </rule>
                </rules>
            </rewrite>
            <handlers>
                <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
            </handlers>
            <aspNetCore processPath="dotnet" arguments=".\sitename.dll" stdoutLogEnabled="false" stdoutLogFile="\\?\%home%\LogFiles\stdout" hostingModel="inprocess" />
        </system.webServer>
    </location>
</configuration>

这是部署后的 web.config 文件。

enter image description here

这是成功的输出:

我的实际 URL 是 https://webappname.azurewebsites.net 并重定向到 Google.com

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.