Pasindu Lakshan
Pasindu Lakshan

Reputation: 81

How to add "api" prefix to every end point in an asp .net core web API

I need to automatically add api/ prefix to every end point in my asp .net core web API. How to do that?

Upvotes: 8

Views: 6918

Answers (5)

Saeb Amini
Saeb Amini

Reputation: 24400

In ASP.NET Core 6+ I had to use one of the below options:

  • Set a global conventional controller route template:

    app.MapControllerRoute(
          name: "default",
          pattern: "api/{controller}/{action}");
    

    Note: for conventional routing to work, your controllers cannot have the ApiController attribute, which enforces attribute routing (second option below).

  • Use a base controller class with your route pattern, and inherit it in all your controllers:

    [ApiController]
    [Route("api/[controller]")]
    public class BaseApiController : ControllerBase
    { }
    
    //This will have the route "api/WeatherForecast"
    public class WeatherForecastController: BaseApiController
    {
         ....
    }
    

    I found this option to be the most flexible, especially if you're using Swagger, as it doesn't support conventional route templates.

  • Use the PathBase middleware:

    app.UsePathBase("/api");
    app.UseRouting():
    

    This one seems to be suggested in plenty of places, however, it's important to note that this does not actually add a prefix to your routes, rather, it is ignoring(effectively) the specified path base in requests, so that they still end up hitting the controllers that don't have /api in their route; those routes without /api will still exist and be valid, resulting in duplicate endpoints.

    The intended purpose of this middleware is to remove an optional prefix from a request. I find using it to achieve what we want here bastardising it.

Upvotes: 1

Andriy B.
Andriy B.

Reputation: 451

Seems you can use a constant.

public static class Consts
{
    public const string DefaultRoute = "api/[controller]";
}

and re-use it everywhere. If you need to change the default route everywhere - just change the constant.

[Route(Consts.DefaultRoute)]
public class TestController : ControllerBase
{
...
}

Upvotes: 2

Rena
Rena

Reputation: 36645

You can custom MvcOptionsExtensions to set route prefix globally instead of change the route attribute manually.

1.custom MvcOptionsExtensions:

public static class MvcOptionsExtensions
{
    public static void UseRoutePrefix(this MvcOptions opts, IRouteTemplateProvider routeAttribute)
    {
        opts.Conventions.Add(new RoutePrefixConvention(routeAttribute));
    }

    public static void UseRoutePrefix(this MvcOptions opts, string
    prefix)
    {
        opts.UseRoutePrefix(new RouteAttribute(prefix));
    }
}
public class RoutePrefixConvention : IApplicationModelConvention
{
    private readonly AttributeRouteModel _routePrefix;
    public RoutePrefixConvention(IRouteTemplateProvider route)
    {
        _routePrefix = new AttributeRouteModel(route);
    }
    public void Apply(ApplicationModel application)
    {
        foreach (var selector in application.Controllers.SelectMany(c => c.Selectors))
        {
            if (selector.AttributeRouteModel != null)
            {
                selector.AttributeRouteModel = AttributeRouteModel.CombineAttributeRouteModel(_routePrefix, selector.AttributeRouteModel);
            }
            else
            {
                selector.AttributeRouteModel = _routePrefix;
            }
        }
    }
}

2:Register in Startup.cs(version before .Net6) or in Program.cs(version beyond .Net 6):

services.AddControllers(o =>{
    o.UseRoutePrefix("api");
});

Or:

builder.Services.AddControllers(o =>{
    o.UseRoutePrefix("api");
});

Upvotes: 8

Pankaj
Pankaj

Reputation: 2744

Make your controller constructor with Route Prefix "api/"

For example lets say your controller class name is CustomerController

[Route("api/[controller]")]
public class CustomerController : ControllerBase
{

}

// This will become api/customer
[HttpGet]
public async Task<ActionResult> GetCustomers()
{
   // Code to get Customers
}

// This will become api/customer/{id}
[HttpGet]
[Route("{id}")]
public async Task<ActionResult> GetCustomerById(int id)
{
   // Code to get Customer by Id
}
    

Upvotes: 2

Vasanth R
Vasanth R

Reputation: 212

we can simply add that in top of the controller like this

[Route("api/[controller]")]
public class TestController : ControllerBase
{
    [HttpGet("version")]
    public IActionResult Get()
    {
        return new OkObjectResult("Version One");
    }
   [HttpGet("Types")]
    public IActionResult GetTypes()
    {
        return new OkObjectResult("Type One");
    }
}

so that you can access like below

....api/Test/version
....api/Test/Types

Upvotes: 0

Related Questions