Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Asp.Net Mvc路由-错误路由_C#_Asp.net_Asp.net Mvc_Asp.net Mvc Routing - Fatal编程技术网

C# Asp.Net Mvc路由-错误路由

C# Asp.Net Mvc路由-错误路由,c#,asp.net,asp.net-mvc,asp.net-mvc-routing,C#,Asp.net,Asp.net Mvc,Asp.net Mvc Routing,我正在尝试路由应用程序中找不到的错误页面,如果任何路由错误,则显示错误路由 下面是我的默认路线 routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); routes.MapRoute( "NotFound",

我正在尝试路由应用程序中找不到的错误页面,如果任何路由错误,则显示错误路由

下面是我的默认路线

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

将其添加到web.config文件中

 <system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
  <remove statusCode="404" />
  <error statusCode="404" responseMode="ExecuteURL" path="/Error" />
  <remove statusCode="500" />
  <error statusCode="500" responseMode="ExecuteURL" path="/Error" />
</httpErrors>
<modules>
  <remove name="FormsAuthentication" />
</modules>

点(.)将导致冲突。使用alpha字符作为参数名称

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

//Catch-All InValid (NotFound) Routes
routes.MapRoute(
    name: "NotFound",
    url: "{*url}",
    defaults: new { controller = "Error", action = "PageNotFound" }
);      
它还允许您在操作中获取参数

public class ErrorController : Controller {
    public ActionResult PageNotFound(string url) { ... }
}

以防您想对该值执行任何其他操作。ie日志记录、审核…等

感谢重播,但它不起作用,当我运行应用程序时,我希望每次打开我的默认路径主页,如果我打开其他路径,如userlist或edit Page,则转到Page404查看