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

前端 未结 3 1050
走了就别回头了
走了就别回头了 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 ...
        }
    }
    
    0 讨论(0)
  • 2021-02-19 09:27

    Example with Action name dynamically:

    [Route("api/{lang}/[controller]/[Action]")]
        [ApiController]
        public class SpatiaController : ControllerBase
        {
            private readonly ISpatialService _service;
            private readonly ILogger<SpatialController> _logger;
    
            public SpatialUnitsIGMEController(ILogger<SpatialController> logger, ISpatialService service) 
            {
                _service = service;
                _logger = logger;
            }
    
    
            [HttpGet]
            public async Task<ActionResult<IQueryable<ItemDto>>> Get50k(string lang)
            {
                var result = await _service.GetAll50k(lang);
                return Ok(result);
            }
    
            [HttpGet("{name}")]
            public async Task<ActionResult<ItemDto>> Get50k(string lang, string name)
            {
                var result = await _service.Get50k(lang, name);
                return Ok(result);
            }
        }
    

    To call these endpoints the following calls would be used:

    https://example.domain.com/api/en/spatial/get50k --> we get all the data in English

    https://example.domain.com/api/en/spatial/get50k/madrid --> we obtain the data of madrid in english

    https://example.domain.com/api/es/spatial/get50k/madrid --> we obtain the data of madrid in spanish

    (using the action and language in the route)

    0 讨论(0)
  • 2021-02-19 09:37

    Another possible solution is:

    // other solution
    [Route("aim/v1/contacts/[Action]")]
    public class aimContactsController : Controller
    {
        [HttpPost("{id}")]
        public IActionResult delete(string id)
        {
            // omitted ...
        }
    }
    
    0 讨论(0)
提交回复
热议问题