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

前端 未结 3 1063
走了就别回头了
走了就别回头了 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:27

    Example with Action name dynamically:

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

提交回复
热议问题