A default MVC4 application will come with a RouteConfig class that looks something like this:
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
My question is, why the MapHttpRoute? This is setting up routing for the new WebApi functionality, but I didn’t choose a WebApi project, just a normal MVC4 project. Nothing in a normal MVC4 project seems to require WebApi.
I’m one of the Microsoft developers behind MVC and WebApi. The reason why we have route registrations for both technologies in the template is that we wanted to make it easier for you to get started adding a Web API controller to your project. Just like with MVC if there are no routes registered, then your
Controllerclass is not going to work. By having those 2 routes we make the getting started experience a bit more developer-friendly.