Routes in ASP.net Core API

I read a lot of topics about API routes in the Asp.net core, but I can't get it to work.

Firstly, this is my controller:

Public class BXLogsController : Controller { //[HttpGet("api/[controller]/ID/{id}", Name = "GetL")] public IActionResult GetById(string id) { if (id.Trim() == "") return BadRequest(); else { Logs l = AccessBase.AccBase.GetLog(id); return Json(l); } } //[HttpGet("api/[controller]/API/{apiname}", Name = "GetLAPI")] public IActionResult GetByAPI(string apiname) { if (apiname.Trim() == "") return BadRequest(); else { List<Logs> lstLogs = AccessBase.AccBase.GetLogsApi(apiname); return Json(lstLogs); } } } 

I tried to use HttpGetAttribute with a path (see Comment), but this does not work.

Therefore, I want to use the MapRoute approach MapRoute but this does not work either.

 app.UseMvc(routes => { routes.MapRoute( name: "LogsId", template: "api/[controller]/ID/{id}", defaults: new { controller = "BXLogs", action = "GetById" }); routes.MapRoute( name: "LogsAPI", template: "api/[controller]/API/{apiname}", defaults: new { controller = "BXLogs", action = "GetByAPI" }); }); 

I must have forgotten something, but I can’t see anything.

Can anybody help me?

+15
source share
1 answer

Try it. You can put the general route prefix on the controller.

 [Route("api/[controller]")] public class BXLogsController : Controller { //GET api/BXlogs/id/blah [HttpGet("ID/{id}", Name = "GetL")] public IActionResult GetById(string id) { ... } //GET api/BXlogs/api/blahapi [HttpGet("API/{apiname}", Name = "GetLAPI")] public IActionResult GetByAPI(string apiname) { ... } } 

read here attribute routing Routing to controller actions

+26
source

Source: https://habr.com/ru/post/1012734/


All Articles