Yasser Shaikh
Yasser Shaikh

Reputation: 47774

Routing in Web Api in ASP.NET MVC 4

I am using web api with ASP.NET MVC 4.

I have the following named controller

Earlier my CustomerApiController was named CustomersController so to access it, I simply had to punch in the following url

localhost/api/Customers

but now I have to keep the api controller name as CustomerApiController. I want to be able to hit the same method using localhost/api/Customers what changes do I have to make ?

I have tried making some changes in the RouteConfig.cs file. I tried adding the following to the RegisterRoutes method, but none of them worked.

routes.MapHttpRoute(
            name: "API Default",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

routes.MapRoute(
            name: "Customers",
            url: "api/customer/",
            defaults: new { controller = "CustomerApi", action = "Get", id = UrlParameter.Optional }
        );

Please can some one guide me on this. Thanks

Upvotes: 7

Views: 5313

Answers (2)

Mike Wasson
Mike Wasson

Reputation: 6622

You could extend DefaultHttpControllerSelector and override GetControllerName to apply a custom rule. The default implementation just returns the value of the "controller" variable from the route data. A custom implementation could map this to some other value. See Routing and Action Selection.

Upvotes: 0

tpeczek
tpeczek

Reputation: 24125

Well there are two issues in your code. You are using MapRoute instead of MapHttpRoute. You should also put the more detailed route first so it will not get swallowed by more generic one:

routes.MapHttpRoute(
    name: "Customer",
    url: "api/Customer/{id}",
    defaults: new { controller = "CustomerApi", action = "Get", id = UrlParameter.Optional }
); 

routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

Now if you want your solution to be more generic (when you have more controllers which need to be modified like this) you can use custom HttpControllerRouteHandler to transform incoming controllers names, this way you will be able to keep default routing.

First you need to create custom HttpControllerRouteHandler:

public class CustomHttpControllerRouteHandler : HttpControllerRouteHandler
{
    protected override IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        requestContext.RouteData.Values["controller"] = requestContext.RouteData.Values["controller"].ToString() + "Api";

        return base.GetHttpHandler(requestContext);
    }
}

Now you can register your HttpRoute like this:

routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
).RouteHandler = new CustomHttpControllerRouteHandler();

This way when you put Customer into URL the engine will treat it like CustomerApi.

Upvotes: 14

Related Questions