ASP.NET MVC RC1路由问题

时间:2009-02-16 22:55:47

标签: asp.net asp.net-mvc

我有一个类似于此的路线表

// Allow browsing categories by name, instead of by ID.
routes.MapRoute("Categories", "Categories/{action}/{name}",
    new { controller = "Categories", action = "Index", name = "" }
);

// All other pages use the default route.
routes.MapRoute("Default", "{controller}/{action}/{id}",
    new { controller = "Applications", action = "Index", id = "" }
);

// Show a 404 error page for anything else.
routes.MapRoute("Error", "{*url}",
    new { controller = "Error", action = "404" }
);

然而,最后的通配符路由没有被调用...如果我输入一个不存在的控制器或URL,它会尝试通配符路由上方的默认路由...从而导致找不到有害资源的ASP。净错误页面...我想要一个自定义错误页面...请帮助

2 个答案:

答案 0 :(得分:1)

如果输入一个不存在的控制器,例如'BadController / SomeAction / 123',它将被第二条路线拾取。

如果前两个路由都没有,那么最后一条路线只会被接收,例如'/SomeRandomFile.aspx'的URL,或其他甚至不接近'{controller} / {action的东西} / {id}'语法。

在我的项目中,我使用web.config文件中的<customErrors mode="RemoteOnly" defaultRedirect="/Error" />作为异常的“全能”规则。假设您没有将请求路由到'/ Error'的规则,则需要一个Error控制器。

然后,您可以使用protected void Application_Error(object sender, EventArgs e)方法在异常情况下执行某些代码。

答案 1 :(得分:1)

为什么不创建标准的ASP .NET自定义错误页面?

<customErrors
       mode="RemoteOnly" 
       defaultRedirect="~/View/Shared/CustomErrorPasge.aspx" 
/>