Lost-Hedgehog
Lost-Hedgehog

Reputation: 29

WebApi Routing - map url to api

I've been googling for about an hour and not found an example so I thought I'd ask here.

I am replacing a standard mvc controller with a webapi and having problems with the routing, I've changed the names because of the organisation but they are still valid.

my webapi is currently called SystemAPI in the controllers folder - I want it to have a different name that the url that points to it ideally.

The url I need to point to it is /v1Controller/{id} I cant change the v1Controller url as it is a fixed point with apps I have no control over

my current coding attempt is

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DocumobiApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        config.Routes.MapHttpRoute(
            name: "V1Controller",
            routeTemplate: "v1Controller/{id}",
            defaults: new { id = RouteParameter.Optional }
            );

        // Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
        // To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
        // For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
        //config.EnableQuerySupport();

        var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().FirstOrDefault();
        jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
    }
}

to which I get the response

"message":"No HTTP resource was found that matches the request URI 'http://localhost:38685/v1Controller'.","messageDetail":"No route providing a controller name was found to match request URI 'http://localhost:38685/v1Controller'"}

I'm sure its something fairly obvious but cant for the life of me figure it out.

Cheers,

Steven

Upvotes: 0

Views: 730

Answers (2)

Kiran
Kiran

Reputation: 57949

As the following error message says, a route match should result in providing value for the controller route variable which Web API depends on for selecting a controller.

No route providing a controller name was found to match request URI 'http://localhost:38685/v1Controller

So you could modify the route like below(notice the default value for controller variable):

config.Routes.MapHttpRoute(
            name: "V1Controller",
            routeTemplate: "v1Controller/{id}",
            defaults: new { id = RouteParameter.Optional, controller="SomeControllerHere" }
            );

Upvotes: 1

Darrel Miller
Darrel Miller

Reputation: 142024

Make sure your action method on your controller has an id with a default value.

Upvotes: 0

Related Questions