在ASP.NET Core 2.2中,如果端点路由不起作用,可能有几个原因。下面是几种可能的解决方法:
ConfigureServices
方法中启用了端点路由:public void ConfigureServices(IServiceCollection services)
{
// 其他配置...
services.AddRouting();
// 其他配置...
}
Configure
方法中使用了端点路由:public void Configure(IApplicationBuilder app)
{
// 其他配置...
app.UseRouting();
// 其他配置...
}
Configure
方法中使用了UseEndpoints
来配置端点路由:public void Configure(IApplicationBuilder app)
{
// 其他配置...
app.UseEndpoints(endpoints =>
{
// 配置端点路由
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index" }
);
});
// 其他配置...
}
[Route("api/[controller]")]
public class MyController : Controller
{
[HttpGet]
[Route("myaction")]
public IActionResult MyAction()
{
// 动作方法逻辑
}
}
ConfigureServices
方法中启用了区域路由:public void ConfigureServices(IServiceCollection services)
{
// 其他配置...
services.AddMvc()
.AddRazorPagesOptions(options =>
{
options.Conventions.AddAreaPageRoute("MyArea", "/MyPage", "/MyPage");
});
// 其他配置...
}
这些解决方法中的一种或多种可能会解决你在ASP.NET Core 2.2中端点路由不起作用的问题。