在Startup.cs我有簡單app.map()不工作MVC核心
app.Map("/Home",x=>x.UseMiddlewareLogic1());
我配置的完整代碼看起來如下圖所示
public void Configure(IApplicationBuilder app)
{
app.Map("/Home",x=>x.UseMiddlewareLogic1());
//app.UseMiddlewareLogic1();
//app.UseMiddlewareLogic2();
app.Run(async context =>
Logic3(context));
}
邏輯一個非常簡單的地圖3只如下所示寫回應
public async Task Logic3(HttpContext obj)
{
await obj.Response.WriteAsync("Logic 3\n");
}
上面的代碼顯示404找不到。 中間件邏輯類是Visual Studio模板中的標準類。我正在使用VS 2017.
public class MiddlewareLogic1
{
private readonly RequestDelegate _next;
public MiddlewareLogic1(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext httpContext)
{
await httpContext.Response.WriteAsync("This is logic123 \n");
await _next(httpContext);
}
}
// Extension method used to add the middleware to the HTTP request pipeline.
public static class MiddlewareLogic1Extensions
{
public static IApplicationBuilder UseMiddlewareLogic1(this IApplicationBuilder builder)
{
return builder.UseMiddleware<MiddlewareLogic1>();
}
}
你的'Configure'方法還有什麼嗎?具體來說,_'app_Map(...)'? – BFree
是的,我確實有app.run(更新後的代碼),我正在寫最後的輸出到響應。 –