【问题标题】:Ignore controller route attribute for a specific action [duplicate]忽略特定操作的控制器路由属性[重复]
【发布时间】:2020-02-11 04:45:34
【问题描述】:

我的控制器上有很多动作,如果可能的话,我只想将特定路由用于一个动作。 我正在寻找一种方法来覆盖在控制器级别定义的路由。

[Route("api/candidate/attached-file")]
public class AttachedFileController : Controller
    {
        //overriding controller route
        [HttpPost("api/candidate/{id}/attached-file")]
        public async Task<IActionResult> AttachFile(string id, [FromBody] AttachedFile file)
        {
            ...
        }

        //using controller route
        [HttpGet("{id}")]
        public ActionResult Get(string id) {}

        [HttpGet]
        public ActionResult GetAll() {}

        ...
    }

【问题讨论】:

    标签: c# asp.net-core routing


    【解决方案1】:

    忽略控制器使用的路由方法如下:

    [Route("/api/customControllerName/getCustomMethodName")]
    public ActionResult GetCustomAll() {
    }
    

    使用 [Route("/")] 会忽略 api 路由寻址。

    [Route("api/candidate/attached-file")]
    public class AttachedFileController : Controller
        {
            //overriding controller route
            [HttpPost("api/candidate/{id}/attached-file")]
            public async Task<IActionResult> AttachFile(string id, [FromBody] AttachedFile file)
            {
                ...
            }
    
            //using controller route
            [HttpGet("{id}")]
            public ActionResult Get(string id) {}
    
            [Route("/api/test/value")]
            public ActionResult GetCustomAll() {
            }
        }
    

    所以,通过调用 api/test/value 地址的 GetCustomAll() 方法会被调用。

    【讨论】:

      猜你喜欢
      • 2017-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-24
      • 2014-08-02
      • 2017-07-03
      • 1970-01-01
      • 2011-12-03
      相关资源
      最近更新 更多