我们正在尝试使用ASP.NET核心中的网址重写功能,但似乎不起作用。
下面是我们在Configure方法中对Startup文件进行编码的方式:
var options = new RewriteOptions()
.AddRewrite(@"^restaurant/(\d+)", $"restaurant/hello", skipRemainingRules: true);
app.UseRewriter(options); //enabled Rewriter
app.UseHttpsRedirection();
//app.UseDefaultFiles();
//app.UseStaticFiles();
app.UseFileServer();
app.UseCookiePolicy();
app.UseRequestLocalization();
app.UseRouting();我们做错了什么吗?
非常感谢。
发布于 2021-11-15 02:55:02
Url重写的配置可以正常工作,您可以尝试在app.UseStaticFiles();之后添加以下中间件
app.Run(context => context.Response.WriteAsync(
$"Rewritten Url: " +
$"{context.Request.Path + context.Request.QueryString}"));你的代码会把context.Request.Path从restaurant/(\d+)改成restaurant/hello,当你添加中间件的时候,你可以看到更改后的url directly.You可以在官方的doc中重写.Here的结果:

https://stackoverflow.com/questions/69960659
复制相似问题