如何在 C# 中的某些路由上抛出自定义错误
How to throw custom errors on some routes in c#
如果我执行类似
的操作,我会收到自定义错误
https://localhost:xxxx/TryStuff
但如果我做类似
https://localhost:xxxx/AbcController/Details/1234/TryStuff
它不会引发自定义错误。它抛出类似
的东西
路由文件:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Error",
url: "{controller}/{action}/{id}",
defaults: new
{
controller = "Error",
action = "NotFound",
id = UrlParameter.Optional
});
你需要一个包罗万象的路线
//Catch-All InValid (NotFound) Routes
routes.MapRoute(
name: "NotFound",
url: "{*url}",
defaults: new { controller = "Error", action = "NotFound" }
);
在所有其他路线之后添加此路线。
之前没有捕获到的任何路由都将匹配此路由并路由到适当的控制器。
如果我执行类似
的操作,我会收到自定义错误https://localhost:xxxx/TryStuff
但如果我做类似
https://localhost:xxxx/AbcController/Details/1234/TryStuff
它不会引发自定义错误。它抛出类似
的东西路由文件:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Error",
url: "{controller}/{action}/{id}",
defaults: new
{
controller = "Error",
action = "NotFound",
id = UrlParameter.Optional
});
你需要一个包罗万象的路线
//Catch-All InValid (NotFound) Routes
routes.MapRoute(
name: "NotFound",
url: "{*url}",
defaults: new { controller = "Error", action = "NotFound" }
);
在所有其他路线之后添加此路线。
之前没有捕获到的任何路由都将匹配此路由并路由到适当的控制器。