7

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

I have the following named controller

  • CustomerController : Controller
  • CustomerApiController : ApiController

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

2 Answers 2

14

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is out of date? url parameter cannot be used in the MapHttpRoute method.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.