Asp.net core 通过中间件未到达最后一个管道
我只是在练习使用ASP.NET核心中间件,下面是我的代码。我用地址http://localhost:5000/location并输出:Asp.net core 通过中间件未到达最后一个管道,asp.net-core,.net-core,async-await,Asp.net Core,.net Core,Async Await,我只是在练习使用ASP.NET核心中间件,下面是我的代码。我用地址http://localhost:5000/location并输出: Buffalo in USA // that's correct, coming from MessageOptions class. Status Code: 200 我的期望是流程如下所示: 打印城市和国家名称,因为路径中有/location。 然后使用next()转到打印状态代码200,它也有一个next(),所以我希望之后它会转到最后一个并打印Hel
Buffalo in USA // that's correct, coming from MessageOptions class.
Status Code: 200
我的期望是流程如下所示:
打印城市和国家名称,因为路径中有/location
。
然后使用next()
转到打印状态代码200,它也有一个next()
,所以我希望之后它会转到最后一个并打印Hello World
,但它不会转到最后一个打印Hello World。
我的问题是为什么
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IOptions<MessageOptions> options)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
// conditional branching in middleware.
app.MapWhen(context => context.Request.Query.ContainsKey("branch"), branch => { branch.UseMiddleware<QueryStringMiddleWare>(); });
app.Use(async (context, next) =>
{
if (context.Request.Path == "/location")
{
MessageOptions opts = options.Value;
await context.Response.WriteAsync($"{opts.CityName} in {opts.CountryName}");
await next();
}
else
{
await next();
}
});
app.Use(async (context, next) =>
{
await context.Response.WriteAsync($"\n Status Code: {context.Response.StatusCode} \n");
await next();
});
app.Use(async (context, next) =>
{
if (context.Request.Method == HttpMethods.Get && context.Request.Query["custom"] == "true")
{
await context.Response.WriteAsync("Hello from Inline Middleware \n");
}
await next();
});
app.UseMiddleware<QueryStringMiddleWare>();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World From Starutp.cs! \n");
});
});
}
public void配置(IApplicationBuilder应用程序、IWebHostEnvironment环境、IOOptions)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
//中间件中的条件分支。
app.MapWhen(context=>context.Request.Query.ContainsKey(“branch”),branch=>{branch.useMidleware();});
应用程序使用(异步(上下文,下一步)=>
{
if(context.Request.Path==“/location”)
{
MessageOptions opts=options.Value;
wait context.Response.WriteAsync($“{opts.CountryName}中的{opts.CityName}”);
等待下一个();
}
其他的
{
等待下一个();
}
});
应用程序使用(异步(上下文,下一步)=>
{
wait context.Response.WriteAsync($“\n状态代码:{context.Response.StatusCode}\n”);
等待下一个();
});
应用程序使用(异步(上下文,下一步)=>
{
if(context.Request.Method==HttpMethods.Get&&context.Request.Query[“custom”]==true”)
{
wait context.Response.WriteAsync(“来自内联中间件的Hello\n”);
}
等待下一个();
});
app.UseMiddleware();
app.UseRouting();
app.UseEndpoints(端点=>
{
endpoints.MapGet(“/”,异步上下文=>
{
wait context.Response.WriteAsync(“来自Starutp.cs的Hello World!\n”);
});
});
}
最后一个匹配像httpget这样的请求http://localhost:5000/
和不匹配的请求,如HTTP GEThttp://localhost:5000/location
最后一个匹配像httpget这样的请求http://localhost:5000/
和不匹配的请求,如HTTP GEThttp://localhost:5000/location
哦,我明白了。。。好的,谢谢。哦,我明白了。。。好的,谢谢。