在Blazor应用的“Startup.cs”文件中的“Configure”方法中,添加以下中间件:
app.Use(async (context, next) =>
{
if (context.Request.Path.Value.EndsWith("/") && context.Request.QueryString.HasValue)
{
context.Request.Path = context.Request.Path.Value.Substring(0, context.Request.Path.Value.Length - 1);
await next();
context.Request.Path = context.Request.Path.Value + "/";
}
else
{
await next();
}
});
这个中间件会检查请求路径的结尾是否有斜杠,同时又有查询字符串。如果是,就会从请求路径中移除末尾的斜杠,接着调用下一个中间件,最后再将斜杠添加回来。这样就可以解决Blazor在查询字符串前添加斜杠的问题。