How to add global route prefix in asp.net core 6 [duplicate]
Asked Answered
M

1

7

Unfortunately, advices for previous versions didn't work How to add global route prefix in asp.net core 3?

app.UsePathBase(new PathString("/api"));

nor this one

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

    public static void UseGeneralRoutePrefix(this MvcOptions opts, string 
    prefix)
    {
        opts.UseGeneralRoutePrefix(new RouteAttribute(prefix));
    }
}
Mclain answered 4/5, 2022 at 11:25 Comment(0)
M
12

Sorry silly me, works fine

app.UsePathBase(new PathString("/api/service"));
app.UseRouting();

Just add app.UseRouting();

I don't mind closing the question if you think that it is appropriate.

Mclain answered 4/5, 2022 at 11:28 Comment(5)
Is this still the right way to add a prefix globally? I have it in same exact order but no luck.Mukerji
The way that is specified above works fine for my projectsMclain
I have this in my controller [ApiController] [Route("v{version:apiVersion}/[controller]")] [ApiVersion("1")] Does that impact/override the UsePathBase?Mukerji
I am not 100% sure but , I am using the similar [Route("users/{userId}")] with [ApiController]Mclain
While this might seemingly be achieving the same behaviour, it is not actually adding a prefix to the endpoint, and you end up with duplicate endpoints. See https://mcmap.net/q/451238/-how-to-prepend-a-common-prefix-to-all-urls-before-the-controller-in-asp-net-apiHypanthium

© 2022 - 2024 — McMap. All rights reserved.