c# - 忽略特定操作的 Controller 路由属性

标签 c# asp.net-core routes

<分区>

我的 Controller 上有很多 Action ,如果可能的话,我想只对一个 Action 使用特定的路线。 我正在寻找一种方法来覆盖在 Controller 级别定义的路由。

[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() {}

        ...
    }

最佳答案

忽略 Controller 的路由方法,如下所示:

[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() 方法。

关于c# - 忽略特定操作的 Controller 路由属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58378809/

相关文章:

c# - 任务并行库 - 阻塞操作?

c# - 在 Windows 服务中使用 ocx 文件

c# - 使用 dotnet restore 的 Docker 导致错误 : GSSAPI operation failed - An unsupported mechanism was requested

asp.net-core - 即使在实现 IdentityServer 之后,ASP.Net Core 应用程序也会重定向到/Account/Login

c# - 名称为( Controller 名称)的 RedirectToAction 无法定位操作

Node.js 和 Express : Routes

c# - 如何在 Google Drive SDK 中获取文件修订的内容?

c# - 添加到字典中的字典

javascript - 捆绑中间件

ruby-on-rails - 设计 Ruby On Rails 4 路线