在ASP.NET Core中,app.UseRouting()和app.UseEndpoints()是两个中间件方法,用于配置路由和终结点。
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// 其他中间件配置...
app.UseRouting();
// 其他中间件配置...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// 其他中间件配置...
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello, World!");
});
endpoints.MapGet("/about", async context =>
{
await context.Response.WriteAsync("About Page");
});
// 添加其他终结点...
});
// 其他中间件配置...
}
app.UseRouting()和app.UseEndpoints()之间的区别在于它们的功能和用途。app.UseRouting()用于配置路由服务,而app.UseEndpoints()用于配置应用程序的终结点。