Search code examples
asp.net-core.net-coreasp.net-core-6.0

How to prepend a common prefix to all URLs before the controller in ASP.NET API?


I'm working on an ASP.NET API project where I have multiple controllers, and I want to prepend a common prefix to all the URLs before the controller route. For example, I want all my API endpoints to have the prefix "/appapi/" followed by the controller name.

I know that I can achieve this by manually adding the prefix in each controller using the [Route]attribute, like [Route("appapi/MyController")]. However, this requires updating every controller, which can be time-consuming and error-prone.

Is there a more efficient and centralized way to add this common prefix to all the URLs without modifying each controller individually? I want to avoid duplicating the prefix in multiple places and have a single configuration or setup that applies it to all controllers.

I'm using ASP.NET 6.0 and have already tried using app.UsePathBase("/appapi") in the Configure method of my Program.cs file, but it didn't prepend the prefix to the URLs as expected.

Any guidance or suggestions on how to achieve this would be greatly appreciated. Thank you!

app.UseRouting(); app.UsePathBase("/appapi");


Solution

  • Is there a more efficient and centralized way to add this common prefix to all the URLs without modifying each controller individually? I want to avoid duplicating the prefix in multiple places and have a single configuration or setup that applies it to all controllers.

    Of course you can, in fact, using app.UsePathBase we can achieve that. But based on your shared code snippet and description it seems that, your configuration is not correct. Within UsePathBase middleware we have to pass our new route value in order to set a global route prefix with all the existing controller.

    Here is the correct way:

    Program.cs:

    app.UsePathBase(new PathString("/appapi"));
    app.UseRouting();
    
    app.Run();
    

    Note: As you can see I am passing the prefix using new route prefix and also your controller should have route name as your controller only.

    Demo Controller:

        [Route("RoutePrefixTest")]
        [ApiController]
        public class RoutePrefixTestController : ControllerBase
        {
            public IActionResult GetCity()
            {
                var checkBaseRoutePath = HttpContext.Request.PathBase;
    
                var cityList = new List<City>()
                {
                    new City { Id = 101, Name = "Alberta" },
                    new City { Id = 102, Name = "Toronto" },
                    new City { Id = 103, Name = "British Comlombia" },
                };
                return Ok(cityList);
            }
        }
    

    Demo Modeel:

    public class City
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    

    Output:

    enter image description here

    enter image description here

    Note: Please refer to this official document here.