URL 使用 Microsoft.AspNetCore.Rewrite 扩展重写 ASP.NET 核心 3 中的静态文件
URL rewriting for static files in ASP.NET core 3 using Microsoft.AspNetCore.Rewrite extension
我在使用静态文件 URL 重写 asp.net 核心 3 时遇到问题。
所以我想去掉 URL 中的 .html 扩展名。
我的 launchSettings.json 看起来像这样:
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "DemoSignUp.html",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
因此,为了将其重写为 "DemoSignUp",我在 Startup.cs 中添加了这些代码行:
app.UseRewriter(new RewriteOptions()
.AddRedirect("DemoSignUp.html", "DemoSignUp")
.AddRewrite("DemoSignUp", "DemoSignUp.html", skipRemainingRules: false));
但是我的 URL 仍然有 .html 扩展名
https://localhost:44319/DemoSignUp.html
您需要将重写器中间件放在静态文件中间件之前,例如:
app.UseRewriter(new RewriteOptions()
.AddRedirect("DemoSignUp.html", "DemoSignUp")
.AddRewrite("DemoSignUp", "DemoSignUp.html", skipRemainingRules: false));
app.UseStaticFiles();//after above rewriter rules
app.UseRouting();
//other middlewares
我在使用静态文件 URL 重写 asp.net 核心 3 时遇到问题。 所以我想去掉 URL 中的 .html 扩展名。
我的 launchSettings.json 看起来像这样:
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "DemoSignUp.html",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
因此,为了将其重写为 "DemoSignUp",我在 Startup.cs 中添加了这些代码行:
app.UseRewriter(new RewriteOptions()
.AddRedirect("DemoSignUp.html", "DemoSignUp")
.AddRewrite("DemoSignUp", "DemoSignUp.html", skipRemainingRules: false));
但是我的 URL 仍然有 .html 扩展名
https://localhost:44319/DemoSignUp.html
您需要将重写器中间件放在静态文件中间件之前,例如:
app.UseRewriter(new RewriteOptions()
.AddRedirect("DemoSignUp.html", "DemoSignUp")
.AddRewrite("DemoSignUp", "DemoSignUp.html", skipRemainingRules: false));
app.UseStaticFiles();//after above rewriter rules
app.UseRouting();
//other middlewares