How do I access RouteTable.Routes.MapHttpRoute?

If anyone has this same issue in C# read on.

I am also using a web forms app and set the routing through the Global.asax file. For me Intellisense was 100% correct and it would not build regardless. To get this to work I had to add the following using directives.

using System.Web.Http;

and

using System.Web.Routing;

Becareful not to use using System.Web.Http.Routing by accident. This wont work.


You should add the reference to System.Web.Http.WebHost assembly and make sure you have

using System.Web.Http;

Why ? MapHttpRoute is defined in two assemblies, in System.Web.Http:

public static System.Web.Http.Routing.IHttpRoute MapHttpRoute(
    this System.Web.Http.HttpRouteCollection routes, string name, string routeTemplate)

Member of System.Web.Http.HttpRouteCollectionExtensions

and in System.Web.Http.WebHost

public static Route MapHttpRoute(
    this RouteCollection routes, string name, string routeTemplate, object defaults);

First one is an Extension on HttpRouteCollection

Second one is an Extension on RouteCollection

So when you have a webforms app, your Routes are defined in a RouteCollection so you need the WebHost version.

Its because architecture allowing WebApi to be hosted also out of IIS. see Hosting WebApi


I found this thread, which indicates that IntelliSense apparently has a problem with this, but if you type something like the following, it will build and run:

RouteTable.Routes.MapHttpRoute("MyApi", "api/{controller}")