Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  • 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

like image 985
Yasser Shaikh Avatar asked Oct 05 '12 08:10

Yasser Shaikh


People also ask

What is routing in MVC Web API?

Routing is how Web API matches a URI to an action. Web API 2 supports a new type of routing, called attribute routing. As the name implies, attribute routing uses attributes to define routes. Attribute routing gives you more control over the URIs in your web API.

What makes it different a Web API routing from MVC routing Web API?

If you are familiar with ASP.NET MVC, Web API routing is very similar to MVC routing. The main difference is that Web API uses the HTTP verb, not the URI path, to select the action. You can also use MVC-style routing in Web API.

How many types of routing are there in Web API?

Web API supports two types of routing: Convention-based Routing. Attribute Routing.

What is routing in REST API?

Routing is a functionally based tag or Uri template used by APIs to match the desired action or methods expected to be executed. There are two types or rather two different types of Routing being used during development.


1 Answers

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.

like image 83
tpeczek Avatar answered Oct 19 '22 13:10

tpeczek