MVC 处理 404 并设置默认错误页面

发布于 2024-10-10 04:37:38 字数 980 浏览 1 评论 0原文

我想在 MVC 中启用默认路由。

我希望每个 404 请求都重定向到 DefaultController DefaultRout()

我发现 我如何制作一个捕获所有路由来处理 ASP.NET MVC 的“404 页面未找到”查询?

但是 {*url}不起作用,我收到 404 并且没有重定向到默认页面。

我的代码:

public static void RegisterRoutes(RouteCollection routes)
{
  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
  routes.IgnoreRoute("{resource}.ascx/{*pathInfo}");
  routes.IgnoreRoute("{resource}.ashx/{*pathInfo}");
  routes.IgnoreRoute("{resource}.gif/{*pathInfo}");

  //http://localhost:4775/BW/A/Tasks
  routes.MapRoute("Pages", "A/{controller}", new { controller = "Tasks", action = "InitPage" });

  routes.MapRoute(
    "404-PageNotFound",
    "{*url}",
    new { controller = "Default", action = "DefaultRout" }
  );
}

我缺少什么?

谢谢

拉斐尔

I'm tying to enable the default routing in MVC.

I want every 404 request to redirect to DefaultController DefaultRout()

I found How can i make a catch all route to handle '404 page not found' queries for ASP.NET MVC?

But {*url} dosen't work i'm getting 404 and not redirecting to the default page.

My code:

public static void RegisterRoutes(RouteCollection routes)
{
  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
  routes.IgnoreRoute("{resource}.ascx/{*pathInfo}");
  routes.IgnoreRoute("{resource}.ashx/{*pathInfo}");
  routes.IgnoreRoute("{resource}.gif/{*pathInfo}");

  //http://localhost:4775/BW/A/Tasks
  routes.MapRoute("Pages", "A/{controller}", new { controller = "Tasks", action = "InitPage" });

  routes.MapRoute(
    "404-PageNotFound",
    "{*url}",
    new { controller = "Default", action = "DefaultRout" }
  );
}

What am I missing?

Thanks

Rafael

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独难免 2024-10-17 04:37:38

您无法使用您的 web.config 吗?我认为这会更容易:

<customErrors mode="On" defaultRedirect="/error/default">
  <error statusCode="403" redirect="/error/restricted"/>
  <error statusCode="404" redirect="/Default/DefaultRoute"/>
  <error statusCode="500" redirect="/error/problem"/>
</customErrors>

Are you unable to use your web.config? I think this would be easier:

<customErrors mode="On" defaultRedirect="/error/default">
  <error statusCode="403" redirect="/error/restricted"/>
  <error statusCode="404" redirect="/Default/DefaultRoute"/>
  <error statusCode="500" redirect="/error/problem"/>
</customErrors>
左耳近心 2024-10-17 04:37:38

在这里我想指出几点。

我注意到您对物理文件使用了许多 IgnoreRoute 条目。您不必这样做,因为框架在路由之前默认会查找与 url 匹配的物理文件。您可以通过将 Global.asax 中的 RouteCollection 上的 RouteExistingFiles 设置为 true 来禁用物理文件匹配。在这种情况下,你还没有这样做。

其次,按照您的设置方式,除 /A/{controller} 之外的任何路由都将被您配置的捕获所有路由(任何以 * 开头的内容都是捕获所有路由)捕获。

我已经尝试过这种配置,它确实捕获了除上述路由之外的所有其他路由。然而,您必须记住的一件事是,上述配置仍将匹配具有以下类型 url 的所有内容:/A/something/,因为第二段将始终与 {controller} 占位符匹配。要仅将此 url 与“任务”控制器匹配,您可以在路由上定义一个约束,如下所示:

routes.MapRoute("Pages", "A/{controller}", new { controller = "Tasks", action = "InitPage" }, new {controller="Home"});

您的 catch all 路由配置中也存在拼写错误。 action =“DefaultRout”应该是action =“DefaultRoute”

希望这有帮助。

There are a few things that I would like to point here.

I notice that you have used many IgnoreRoute entries for physical files. You don't have to do that as the framework looks for the physical files matching the url by default before routing it. You can disable the physical file matching by turning RouteExistingFiles to true on the RouteCollection in Global.asax. In this case you haven't done that.

Secondly, the way you have set it up, any route but /A/{controller} will be caught by the catch all route (anything starting with * is a catch all route) that you have configured.

I have tried this configuration and it does catch all the other routes except the one mentioned above. One thing you have to keep in mind, however, is that the above configuration will still matching everything with the following type of url: /A/something/ because the second segment will always match the {controller} placeholder. To only match this url with the "Tasks" controller, you can define a constraint on the route as the following:

routes.MapRoute("Pages", "A/{controller}", new { controller = "Tasks", action = "InitPage" }, new {controller="Home"});

There is also a spelling mistake in your catch all route configuration. action = "DefaultRout" should be action = "DefaultRoute"

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文