中间件
public class Startup
{
....
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
//配置Http请求管道
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting(); //路由中间件
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
});
});
}
}
端点 endpoint
端点 就是http请求的Url的结尾那部分,这部分会被中间件进行处理
路由中间件
app.UseRouting(); //路由中间件
端点中间件
app.UseEndpoints 配置为使用MVC
//配置Http请求管道
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
/* endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
});*/
//endpoints.MapControllers();//使用路由特性,放置在 Cotroller/Action上时使用
endpoints.MapControllerRoute(
"default",
"{controller=Home}/{action=Index}/{id?}");
});
}
其它常用中间件
//配置Http请求管道
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseStaticFiles(); //静态文件中间件,使得请求可以访问静态文件,比如图片,css,html
//app.UseHttpsRedirection(); //将http请求转化为https请求
//app.UseAuthentication(); //身份认证
app.UseRouting(); //路由中间件
app.UseEndpoints(endpoints =>
{
/* endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello World!");
});*/
//endpoints.MapControllers();//使用路由特性,放置在 Cotroller/Action上时使用
endpoints.MapControllerRoute(
"default",
"{controller=Home}/{action=Index}/{id?}");
});
}
}