trx trx - 16 days ago 4
ASP.NET (C#) Question

'System.Web.Routing.RouteCollection' does not contain a definition for 'MapHttpRoute'

In the RoutingConfig.cs I added the below code for handling the Handling HTTP 404 Error in ASP.NET Web API

public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

routes.MapRoute(
name: "Error404",
routeTemplate: "{*url}",
defaults: new { controller = "Error", action = "Handle404" }
);
}
}


It throws the below error like


'System.Web.Routing.RouteCollection' does not contain a definition for
'MapHttpRoute' and no extension method 'MapHttpRoute' accepting a
first argument of type 'System.Web.Routing.RouteCollection' could be
found (are you missing a using directive or an assembly reference?)


I tried to ass the namespace
System.Web.Routing.RouteCollection
but it is not working

Answer

Use the following

//Catch-All InValid (NotFound) Routes
routes.MapRoute(
    name: "Error404",
    url: "{*url}",
    defaults: new { controller = "Error", action = "Handle404" }
);

Make sure that this is the last route to be mapped as this is a catch-all route.