Routes with different controllers but same action name fails to produce wanted urls

前端 未结 3 1046
走了就别回头了
走了就别回头了 2021-02-19 08:50

I am trying to set up a API for my MVC web app that will have a lot of routes but much of the same part for each one. Basically a CRUD for each area. I am also setting it up t

3条回答
  •  隐瞒了意图╮
    2021-02-19 09:22

    Both your routes are named the same, this cannot work in ASP.NET Core MVC.

    I'm not talking about the methods naming, but about routes naming. You called both your routes with the same identifier Name = "delete" inside the HttpPost attribute. Route names in MVC uniquely identifies a route template.

    From what I can see you do not really need to identify your routes, but only to distinguish different URIs. For this reason you may freely remove the Name property of HttpPost attribute on your action methods. This should be enough for ASP.NET Core router to match your action methods.

    If you, instead, what to revert using only attribute routing you better change your controller to the following:

    // other code omitted for clarity
    [Route("aim/v1/contacts/")]
    public class aimContactsController : Controller
    {
        [HttpPost("delete/{id}")]
        public IActionResult delete(string id)
        {
            // omitted ...
        }
    }
    

提交回复
热议问题