【问题标题】:ASP.NET MVC RC1 Routing problemASP.NET MVC RC1 路由问题
【发布时间】:2009-02-16 22:55:47
【问题描述】:

我有一个类似的路由表

// 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 的信息资源。网络错误页面...我想要一个自定义错误页面...请帮助

【问题讨论】:

    标签: asp.net asp.net-mvc


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      最后一条路线只有在前两条都没有的情况下才会被拾取,例如 '/SomeRandomFile.aspx' 的 URL,或者甚至不接近 '{controller}/{action 的其他东西}/{id}' 语法。

      在我的项目中,我使用 web.config 文件中的 &lt;customErrors mode="RemoteOnly" defaultRedirect="/Error" /&gt; 作为例外情况的“全能”规则。假设您没有将请求路由到“/Error”的规则,您将需要一个错误控制器。

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

      【讨论】:

        猜你喜欢
        • 2010-10-05
        • 2013-11-13
        • 2011-11-30
        • 2013-06-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多