【问题标题】:url multiple slashes right after domain/port cause 404, in asp.net core v5.0在 asp.net core v5.0 中,在域/端口导致 404 之后,url 多个斜杠
【发布时间】:2021-03-06 19:57:03
【问题描述】:

问题
在 .Net Core v5.0 应用中:

  • 如何捕获 URL,例如:“http://localhost:5000//”或“http://localhost:5000///”??
  • 有没有办法通过路由配置捕获此类 URL?
  • 或者,有没有办法在 IIS 中捕获此类 URL?

这些 URL 在 .NET Core v2.1 应用中是正常的(200,而不是 404)。


问题
http://localhost:5000 - //200 OK
http://localhost:5000/ - //200 OK
http://localhost:5000// - //404(多于 1 个斜线:导致 404
http://localhost:5000/// - //404

控制台信息:(注意localhost:5000后面的双斜杠“//”

info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
      Request finished HTTP/1.1 GET http://localhost:5000// - - - 404 0 - 12.1067ms

同时,可以捕获和处理在 URL 路径中间(不是紧跟在域/端口之后)带有多个斜杠的 URL。


环境
1.asp.net core v5.0
2.路由配置:

app.UseRouting();
app.UseMvc(routes => //attribute routing is always available
{
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");
});

3.Program.cs

public static void Main(string[] args)
{
    Console.Title = "WebApplication1";

    var host = CreateHostBuilder(args).Build();
    host.Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
        });

4.web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\WebApplication1.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
    </system.webServer>
  </location>
</configuration>

【问题讨论】:

  • 在.net core v2.1、v3.1、v5.0中测试,“localhost:5000//”在v2.1中显示正常首页索引页面,而在v3.1和v5中显示404 .0.

标签: c# asp.net asp.net-core http-status-code-404 url-routing


【解决方案1】:

或者,有没有办法在 IIS 中捕获此类 URL?

您可以在 IIS 中使用 url rewrite 将 \\\ 重写为 ""。

您可以使用此extension 安装 url 重写。

然后你可以添加下面的 url 重写规则:

<rewrite>
  <rules>
    <rule name="Remove multiple slashes" stopProcessing="true">
      <match url=".*" />
      <action type="Redirect" url="{REQUEST_URI}" />
      <conditions>
        <add input="{UNENCODED_URL}" pattern="(.*?)[/]{2,}$" />
      </conditions>
    </rule>
  </rules>
</rewrite>

结果:


将以下代码添加到配置方法中:

        var options = new RewriteOptions()
.AddRedirect(@"(.*?)[/]{2,}$", "/");
        app.UseRewriter(options);

结果:

【讨论】:

  • 这确实解决了 .net core v5.0 的 IIS 问题。我想知道有没有办法在代码中捕获这样的 url?在.net core v2.1中,这样的url在没有url-rewrite的情况下不会导致404。
  • 更新:将模式修改为&lt;add input="{UNENCODED_URL}" pattern="[/]{2,}" /&gt;,以捕获中间有多个斜杠的url(不仅仅是结尾)。
  • 在我看来,我们可以在代码中捕获这个url,我们可以使用自定义的中间件来监控url,如果url包含///,我们可以将其修改为/。 asp.net core 还包含内置的 url 重写中间件,我们可以使用它来代替使用 url 重写规则。更多细节,你可以参考我的更新回答。
猜你喜欢
  • 2011-07-21
  • 2013-11-03
  • 1970-01-01
  • 2019-02-04
  • 1970-01-01
  • 1970-01-01
  • 2021-03-29
  • 2021-09-22
  • 2016-11-28
相关资源
最近更新 更多