【发布时间】:2020-04-27 12:09:55
【问题描述】:
实际上我正在使用 .NET Core 3,
我的控制器的路由如下:
[ApiController]
[Route("customers/app")]
public class CustomerAppController : ControllerBase
{
}
我想做的是拥有一个基本控制器,我可以在其中为所有控制器放置许多我想要的东西,包括路由前缀,如下所示:
[Route("api/v1/[controller]")]
public class CoreController : ControllerBase
{
protected virtual Object HandleException(Exception ex, string path, ILogger logger)
{
logger.LogError(ex, "Unhandled exception.");
return StatusCode(5000, new { ex.Message });
}
}
那么控制器将是:
[ApiController]
[Route("customers/app")]
public class CustomerAppController : CoreController
{
//TODO: put actions here
public override Object HandleException(Exception ex, string path, ILogger logger) { }
}
我怎样才能做到这一点?
我发现我可以使用这个方法:
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UsePathBase("/api/v1");
//other things
}
现在我可以使用localhost/api/v1/randompath 调用我的api,但如果我使用localhost/randompath 也可以使用
【问题讨论】:
标签: c# .net-core asp.net-web-api2