我想在我的中间件内部映射一个url,但代码永远不会执行,我总是遇到404错误。如果我在中间件之外做同样的事情,一切都按预期工作。如何从中间件中注册映射?
请参阅以下代码中的评论:
public partial class Startup
{
internal void Configure(IAppBuilder app)
{
app.Use(typeof(TestMiddleware), app);
// this works just fine: http://myapp/test
app.Map("/test", config =>
{
config.Run(context =>
{
context.Response.ContentType = "text/html";
return context.Response.WriteAsync("<html><body>test</body></html>");
});
});
}
class TestMiddleware : OwinMiddleware
{
public TestMiddleware(OwinMiddleware next, IAppBuilder app)
: base(next)
{
app.Map("/yup", config =>
{
config.Run(context =>
{
// this never executes. always 404 error: http://myapp/yup
context.Response.ContentType = "text/html";
return context.Response.WriteAsync("<html><body>yup</body></html>");
});
});
}
public override Task Invoke(IOwinContext context)
{
return base.Next.Invoke(context);
}
}
}