我如何让基本控制器的网络核心3中具有前缀路由

问题描述 投票:0回答:1

实际上我正在使用.NET Core 3,

而且我的控制器具有这样的路由:

[ApiController]
[Route("customers/app")]
public class CustomerAppController : ControllerBase
{
}

我想做的是拥有一个Base控制器,我可以在其中为所有控制器放置许多我想要的东西,包括路由前缀,如下所示:

[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也可以使用它>

实际上,我正在使用.NET Core 3,并且我的控制器具有这样的路由:[ApiController] [Route(“ customers / app”)]公共类CustomerAppController:ControllerBase {}我会怎么做...] >

c# .net-core asp.net-web-api2
1个回答
0
投票

将Route attr添加到您的控制器并在那儿设置路径。

[Route("api/v1/[controller]")]
[ApiController]
© www.soinside.com 2019 - 2024. All rights reserved.