为什么自定义中间件仍然 运行?
Why is Custom Middleware Still Being Run?
平台
.Net Core 2.2 网络应用程序
场景
我编写了一些自定义中间件,目的是仅当我的请求路径与指定位置匹配时才执行。当它运行时,它会从数据库中查找并检查一段数据。在某些情况下,中间件会将用户重定向到一个新的 url 而不是让他们查看请求的
问题
中间件好像有缓存什么的。我不明白。当我请求 localhost:6000/url-that-should-work
时,我神奇地在另一个 url 获得了错误的页面。但是,当我使用 Fiddler 时,它不是重定向。这就像那是我实际请求的页面。奇怪的是 a) 我在 Startup.cs
中注释掉了我的中间件代码,b) 当我进入隐身模式时,该行为按预期工作。此外,如果我将调试端口更改为不同的端口,它也能正常工作。另外,当我在我的中间件中放置断点时,如果我转到/url-that-should-work
,请求路径是错误的url。
代码
Startup.cs
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
//app.UseEndpointRouting();
//app.UseWhen(context => context.Request.Path.StartsWithSegments("/move/booking"), appBuilder =>
//{
// appBuilder.UseMoveBookingWorkflowPermission();
//});
app.UseMvc();
不应执行的中间件重定向行
context.Response.Redirect(context.Request.PathBase + previousPath + context.Request.QueryString, true);
根据 .Net 核心文档
The order of your middlewares can change their functionality,
security and performance.
例如在 Startup.cs
的配置中
public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles();
app.UseResponseCompression();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
此中间件订单未压缩静态文件。可以压缩 Razor Pages 响应。这个简单的例子展示了功能将如何改变。
Configure 方法中的顺序很重要,它可以做出响应,也可以将请求传递给下一个中间件。
如果仍然存在问题,您可以重写 URL 中间件。
var options = new RewriteOptions()
.AddRewrite(//some options);
app.UseRewriter(options);
希望对您有所帮助!
这是 "I'm stupid" 时刻之一。问题是当我重定向响应时,我指定我希望它是永久的。
旧
context.Response.Redirect(context.Request.PathBase + previousPath + context.Request.QueryString, true);
新建
context.Response.Redirect(context.Request.PathBase + previousPath + context.Request.QueryString, false);
平台
.Net Core 2.2 网络应用程序
场景
我编写了一些自定义中间件,目的是仅当我的请求路径与指定位置匹配时才执行。当它运行时,它会从数据库中查找并检查一段数据。在某些情况下,中间件会将用户重定向到一个新的 url 而不是让他们查看请求的
问题
中间件好像有缓存什么的。我不明白。当我请求 localhost:6000/url-that-should-work
时,我神奇地在另一个 url 获得了错误的页面。但是,当我使用 Fiddler 时,它不是重定向。这就像那是我实际请求的页面。奇怪的是 a) 我在 Startup.cs
中注释掉了我的中间件代码,b) 当我进入隐身模式时,该行为按预期工作。此外,如果我将调试端口更改为不同的端口,它也能正常工作。另外,当我在我的中间件中放置断点时,如果我转到/url-that-should-work
,请求路径是错误的url。
代码
Startup.cs
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
//app.UseEndpointRouting();
//app.UseWhen(context => context.Request.Path.StartsWithSegments("/move/booking"), appBuilder =>
//{
// appBuilder.UseMoveBookingWorkflowPermission();
//});
app.UseMvc();
不应执行的中间件重定向行
context.Response.Redirect(context.Request.PathBase + previousPath + context.Request.QueryString, true);
根据 .Net 核心文档
The order of your middlewares can change their functionality, security and performance.
例如在 Startup.cs
的配置中public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles();
app.UseResponseCompression();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
此中间件订单未压缩静态文件。可以压缩 Razor Pages 响应。这个简单的例子展示了功能将如何改变。 Configure 方法中的顺序很重要,它可以做出响应,也可以将请求传递给下一个中间件。
如果仍然存在问题,您可以重写 URL 中间件。
var options = new RewriteOptions()
.AddRewrite(//some options);
app.UseRewriter(options);
希望对您有所帮助!
这是 "I'm stupid" 时刻之一。问题是当我重定向响应时,我指定我希望它是永久的。
旧
context.Response.Redirect(context.Request.PathBase + previousPath + context.Request.QueryString, true);
新建
context.Response.Redirect(context.Request.PathBase + previousPath + context.Request.QueryString, false);