可配置的控制器路由前缀

huangapple go评论52阅读模式
英文:

Configurable route prefix for controller

问题

我正在使用ASP.NET Core 6,尝试使我的API控制器的基本路径可配置(以便用户可以选择自己的基本路径,以避免与其他控制器发生冲突)。

我尝试设置以下路由:

string configurablePrefix = "/temp";
endpoint.MapControllerRoute(
    name: "MyRouteName",
    pattern: configurablePrefix + "/{action=MyDefaultAction},
    defaults: new { controller = "MyController" });

其中MyController定义如下:

[ApiController]
public class MyController : ControllerBase
{
    [HttpGet("MyDefaultAction")]
    public IActionResult MyDefaultAction()
    {
        return new JsonResult("Hello");
    }
}

这在启动过程中不会引发错误,但当我访问https://localhost/temp/MyDefaultAction时,我收到了404错误。

如何使其工作,以便MyController中的操作可访问用户选择的任何启动路径(即更改以响应/othertemp/MyDefaultAction而不是)?

英文:

I'm using ASP.NET Core 6 and trying to have the base path of my API controller be configurable (so that users can choose the base path themselves to avoid conflicts with other controllers).

I tried setting up the following route:

string configurablePrefix = "/temp";
endpoint.MapControllerRoute(
    name: "MyRouteName",
    pattern: configurablePrefix + "/{action=MyDefaultAction},
    defaults: new { controller = "MyController" });

Where MyController is defined like this:

[ApiController]
public class MyController : ControllerBase
{
    [HttpGet("MyDefaultAction")]
    public IActionResult MyDefaultAction()
    {
        return new JsonResult("Hello");
    }
}

This causes no errors during startup, but when I access `https://localhost/temp/MyDefaultAction I get a 404

How can I get this to work so that actions in MyController are accessible on whatever start path the user chooses (i.e. change it to respond to /othertemp/MyDefaultAction instead)?

答案1

得分: 1

从您的代码中可以看出,您正在使用 ApiController,这意味着您不能通过在 Startup.csProgram.cs 中设置相应的路由来路由它。

ApiController 必须使用属性路由,属性路由的优先级高于传统路由,因此它将覆盖您定义的传统路由。

您可以选择使用属性路由来定义控制器名称为 temp,以便在 ApiController 中匹配相应的端点:

[Route("temp")]
[ApiController]
public class MyController : ControllerBase
{
    [HttpGet("MyDefaultAction")]
    public IActionResult MyDefaultAction()
    {
        return new JsonResult("Hello");
    }
}

测试结果:

可配置的控制器路由前缀

或者使用 MVC 控制器:

public class MyController : Controller
{
    [HttpGet]
    public IActionResult MyDefaultAction()
    {
        return new JsonResult("Hello");
    }
}

路由:

string configurablePrefix = "/temp";
endpoint.MapControllerRoute(
    name: "MyRouteName",
    pattern: configurablePrefix + "/{action}",
    defaults: new { controller = "My", action = "MyDefaultAction" });

测试结果:

可配置的控制器路由前缀

参考链接:使用 ASP.NET Core 创建 Web API

英文:

From your code, you are using ApiController, which cannot be routed by setting the corresponding route in Startup.cs or Program.cs.

ApiController must have attribute routing, and attribute routing has a higher priority than conventional routing, so it will override the conventional routing you defined.

You can choose to use attribute routing to define the controller name as temp, so that the corresponding endpoint can be matched in ApiController:

[Route("temp")]
[ApiController]
public class MyController : ControllerBase
{
    [HttpGet("MyDefaultAction")]
    public IActionResult MyDefaultAction()
    {
        return new JsonResult("Hello");
    }
}

Test Result:

可配置的控制器路由前缀

Or use an MVC controller:

public class MyController : Controller
{
    [HttpGet]
    public IActionResult MyDefaultAction()
    {
        return new JsonResult("Hello");
    }
}

Routing:

string configurablePrefix = "/temp";
endpoint.MapControllerRoute(
    name: "MyRouteName",
    pattern: configurablePrefix + "/{action}",
    defaults: new { controller = "My", action = "MyDefaultAction" });

Test Result:

可配置的控制器路由前缀

reference link: Create web APIs with ASP.NET Core.

huangapple
  • 本文由 发表于 2023年2月9日 00:00:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75388462.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定