1
votes

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

2

2 Answers

2
votes

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.

0
votes

because the following URI does not match, because it lacks the "api" segment.

https://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

RouteTemplates are configured for routing incoming requests.