Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Web.Routing.RouteCollection doesn't contain a definiton for "MapHttpRoute" - VS 2012, Web API

Tags:

I've just installed Visual Studio 2012 and started checking out MVC 4 and Web Api. I've created a new MVC 4 app with Web API. Based on some examples and tutorials I started configuring my routes with:

routes.MapHttpRoute(                 name: "Controller only",                 routeTemplate: "api/{controller}"             ); 

However, that results in an error that RouteCollection doesn't contain a definition for MapHttpRoute. Am I missing some DLLs installed? As far as I could see I have all the right DLLs and version installed.

like image 627
sTodorov Avatar asked Sep 17 '12 08:09

sTodorov


People also ask

Which types of routing is supported in Web API?

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.

Which of the following method names are allowed in Web API for get?

Action method name can be the same as HTTP verbs like Get, Post, Put, Patch or Delete as shown in the Web API Controller example above. However, you can append any suffix with HTTP verbs for more readability. For example, Get method can be GetAllNames(), GetStudents() or any other name which starts with Get.


2 Answers

I have solved it by adding a reference to System.Web.Http in RouteConfig.cs

like image 183
sTodorov Avatar answered Dec 07 '22 23:12

sTodorov


Try referencing: System.Web.Http and System.Web.Http.WebHost.dll

Then add a using statement for namespace: System.Web.Http

Note: It could also be that your intellisense is acting up. In which case just go ahead and type your code manually and build.

Hope that helps

like image 44
Houdini Sutherland Avatar answered Dec 08 '22 01:12

Houdini Sutherland