'Global route for all ApiControllers in .Net 6
In all of my projects i put this code in top my controllers :
[Route("api/[controller]/[action]")]
[ApiController]
public class ValuesController : ControllerBase
{
[HttpGet]
public IActionResult GetTest1()
{
return Ok();
}
[HttpGet]
public IActionResult GetTest2()
{
return Ok();
}
[HttpPost]
public IActionResult PostTest1(string Request)
{
return Ok("value was " + Request);
}
}
So i can call my APIs with action name without specify each action route, Like below picture from my swagger :
This work fine but i need to put this route top of all my ApiControllers in my project, When i decide to change all of my route to new one, i need to replace all RouteAttributes in my project.
I look for global solution, for Example something like this in my program.cs file :
app.MapControllerRoute(
name: "default",
pattern: "api/{controller}/{action}");
Problem is i can't make this code work when i delete RouteAttribute from my APIs.
Solution 1:[1]
I search few hours for this and after all i didn't find any simple solution for creating good, simple, fast global route for APIs, Then i tried on my own.
Solution 1 : You can create a base class for your APIs and put your route in that file, Then you only inherit from that class in all of your APIs.
APIBase.cs file :
[ApiController]
[Route("api/[controller]/[action]")]
public class APIBase: ControllerBase
{
}
Next step : If you need have id (or any other parameter) in your URL so you should add RouteAttribute only for that method.
ValuesController.cs file :
public class ValuesController : APIBase
{
[HttpGet]
public IActionResult GetTest1()
{
return Ok();
}
[HttpGet]
[Route("{id?}")]
public IActionResult GetTest2(int id)
{
return Ok("value was " + id);
}
[HttpPost]
public IActionResult PostTest1(string Request)
{
return Ok("value was " + Request);
}
}
Edit :
Solution 2 : You can create a string const in your project and use it in all APIs (Suggested by @Serge)
HelperStatic.cs file :
public static class HelperStatic
{
public const string BaseAPIRoute = "api/[controller]/[action]";
}
ValuesController.cs file :
[ApiController]
[Route(HelperStatic.BaseAPIRoute)]
public class ValuesController : ControllerBase
{
[HttpGet]
public IActionResult GetTest1()
{
return Ok();
}
[HttpGet]
[Route("{id?}")]
public IActionResult GetTest2(int id)
{
return Ok("value was " + id);
}
[HttpPost]
public IActionResult PostTest1(string Request)
{
return Ok("value was " + Request);
}
}
And your swagger will be like this (on both solution) :
Solution 2:[2]
Just followed this simple tutorial on Global route Prefix You will as well have to remove the route prefix on top of the controller after adding the global route prefix to your program.cs file.
my progam.cs
builder.Services.AddMvc(opt =>
{
opt.UseCentralRoutePrefix(new RouteAttribute("core/v1/[controller]/[action]"));
}
).AddControllersAsServices().AddNewtonsoftJson();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Denis Michael Kamukama |