WebAPI - 404 Not Found (MapHttpRoute)

336 Views Asked by At

I'm trying to call ASP.NET Core APIs that starts with "webapi/v1" by using a ASP.NET WebAPI : I'm using a custom DelegatingHandler to handle "webapi/v1..." requests and call the APIs but only these URL Works:

/webapi/v1/current -> OK

/webapi/v1/users/plant/{plantId}?PageSize=10&PageIndex=1 -> OK

/webapi/v1/plants/{plantId}/user/{userId}/app/{appId}/{role} -> KO

My WebapiConfig.cs contains this code at the moment:

    public static void Register(HttpConfiguration config)
    {
        ApiDelegatingHandler handler = new ApiDelegatingHandler()
        {
            InnerHandler = new HttpClientHandler()
        };

        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "webapi/v1/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional, action = RouteParameter.Optional },
            constraints: null,
            handler: handler             
        );
    }

Am I forced to add a new "config.Routes.MapHttpRoute" to handle "complex" routes ? Isn't it possible to map routes that starts with "webapi/v1" ?

What I tried to do was registering the custom DelegatingHandler globally by adding config.MessageHandlers.Add(handler); and then write some code in it to ignore requests that do not starts with my pattern but it was unsuccessful (404.0 Not Found)

1

There are 1 best solutions below

0
On

I finally found out :) In WebApiConfig.cs I had to use "*" like that :

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "webapi/v1/{controller}/{*url}",
            defaults: new { url = RouteParameter.Optional },
            constraints: null,
            handler: handler
        );