【发布时间】: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