在RoutingConfig.cs中,我添加了以下代码来处理ASP.NET Web API中的处理HTTP 404错误
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" }
);
}
}
它会抛出以下错误,如
' System.Web.Routing.RouteCollection'不包含的定义 ' MapHttpRoute'没有扩展方法' MapHttpRoute'接受一个 类型' System.Web.Routing.RouteCollection'的第一个参数可能 发现(您是否缺少using指令或程序集引用?)
我试图对名称空间System.Web.Routing.RouteCollection
进行处理,但它无效
答案 0 :(得分:1)
使用以下
//Catch-All InValid (NotFound) Routes
routes.MapRoute(
name: "Error404",
url: "{*url}",
defaults: new { controller = "Error", action = "Handle404" }
);
确保这是要映射的最后一条路线,因为这是一条全能路线。
答案 1 :(得分:0)
因为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
RouteTemplate
已配置为路由传入请求。