I can't seem to figure this out. I'm experimenting with MVC Beta and am trying to implement a catchall route such that if the user enters mysite.com/blah instead of mysite.com/home/index it will hit the "Error" route.
我似乎无法弄清楚这一点。我正在尝试使用MVC Beta,我正在尝试实现一个捕获路线,这样如果用户输入mysite.com/blah而不是mysite.com/home/index,它将会出现“错误”路径。
Unfortunately it seems that the "Default" route always catches "blah" first. In fact the only route I've been able to get to the "Error" route with is blah/blah/blah/blah.
不幸的是,似乎“默认”路线总是首先捕捉到“等等”。事实上,我唯一能够进入“错误”路线的路线是blah / blah / blah / blah。
Is this the way it's supposed to work, because I've seen other examples that have the "Default" and "Error" route set up just like this and it seems that if they were to type in a controller that doesn't exist it would hit the "Error" route.
这是它应该工作的方式,因为我已经看到其他示例具有“默认”和“错误”路由设置就像这样,似乎如果他们要键入不存在它的控制器会点击“错误”路线。
Is there something I'm missing (very possible) or will I just have to create a specific route for each controller?
有什么我缺少的(非常可能)或者我只需要为每个控制器创建一个特定的路由?
Code I'm using:
我正在使用的代码:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
routes.MapRoute(
"Error",
"{*catchall}",
new { controller = "Base", action = "Error", id = "404" }
);
Thank you, Jeff
谢谢杰夫
4 个解决方案
#1
3
MVC routes are checked in the order that they are entered.
MVC路由按输入顺序进行检查。
Mysite/blah will be found by the default route. The controller will be blah, and the action is index.
默认路由将找到Mysite / blah。控制器将是等等,动作是索引。
When you entered the mysite/blah/blah/blah/blah route you gave it a route it could not map the default route to and then your catchall route was called.
当你输入mysite / blah / blah / blah / blah路线时,你给它一条路线,它无法将默认路线映射到,然后调用你的catchall路线。
For those other examples, did you notice if they had some error filters setup? I'm pretty sure the default asp.net mvc site has some error handling attributes on the pages already.
对于其他示例,您是否注意到他们是否设置了错误过滤器?我很确定默认的asp.net mvc网站已经在页面上有一些错误处理属性。
#2
6
Your first route will catch the most urls since you have defaults for the elements, you can visualize this using the route debugger from Phil Haack, see the link:
您的第一条路线将捕获最多的网址,因为您有元素的默认值,您可以使用Phil Haack的路由调试器将其可视化,请参阅链接:
路由调试器
#3
5
In order to handle errors I used the Application_Error event in one of my projects:
为了处理错误,我在我的一个项目中使用了Application_Error事件:
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
HttpException httpException = exception as HttpException;
if (httpException != null)
{
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Error");
routeData.Values.Add("action", "HttpError500");
if (httpException.GetHttpCode() == 404)
{
routeData.Values["action"] = "HttpError404";
}
Server.ClearError();
Response.Clear();
IController errorController = new ErrorController();
errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
}
}
#4
0
This can also help when dealing with MVC catchall problems:
这在处理MVC捕获问题时也有帮助:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{*id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
That is, where it says {id}
, change it to {*id}
. This allows the final id parameter to consume as much additional path as might be passed in. The default rule accepts this:
也就是说,在{id}的位置,将其更改为{* id}。这允许最终的id参数消耗尽可能多的传入路径。默认规则接受:
/person/name/joe
/人/姓名/乔
But not this:
但不是这个:
/products/list/sortby/name
/产品/列表/ sortby /名称
The second URL will throw a 404 without this modification to the route.
第二个URL将抛出404而不对路径进行此修改。
#1
3
MVC routes are checked in the order that they are entered.
MVC路由按输入顺序进行检查。
Mysite/blah will be found by the default route. The controller will be blah, and the action is index.
默认路由将找到Mysite / blah。控制器将是等等,动作是索引。
When you entered the mysite/blah/blah/blah/blah route you gave it a route it could not map the default route to and then your catchall route was called.
当你输入mysite / blah / blah / blah / blah路线时,你给它一条路线,它无法将默认路线映射到,然后调用你的catchall路线。
For those other examples, did you notice if they had some error filters setup? I'm pretty sure the default asp.net mvc site has some error handling attributes on the pages already.
对于其他示例,您是否注意到他们是否设置了错误过滤器?我很确定默认的asp.net mvc网站已经在页面上有一些错误处理属性。
#2
6
Your first route will catch the most urls since you have defaults for the elements, you can visualize this using the route debugger from Phil Haack, see the link:
您的第一条路线将捕获最多的网址,因为您有元素的默认值,您可以使用Phil Haack的路由调试器将其可视化,请参阅链接:
路由调试器
#3
5
In order to handle errors I used the Application_Error event in one of my projects:
为了处理错误,我在我的一个项目中使用了Application_Error事件:
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
HttpException httpException = exception as HttpException;
if (httpException != null)
{
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Error");
routeData.Values.Add("action", "HttpError500");
if (httpException.GetHttpCode() == 404)
{
routeData.Values["action"] = "HttpError404";
}
Server.ClearError();
Response.Clear();
IController errorController = new ErrorController();
errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
}
}
#4
0
This can also help when dealing with MVC catchall problems:
这在处理MVC捕获问题时也有帮助:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{*id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
That is, where it says {id}
, change it to {*id}
. This allows the final id parameter to consume as much additional path as might be passed in. The default rule accepts this:
也就是说,在{id}的位置,将其更改为{* id}。这允许最终的id参数消耗尽可能多的传入路径。默认规则接受:
/person/name/joe
/人/姓名/乔
But not this:
但不是这个:
/products/list/sortby/name
/产品/列表/ sortby /名称
The second URL will throw a 404 without this modification to the route.
第二个URL将抛出404而不对路径进行此修改。