本文介绍了如何为 ASP.NET MVC 处理“未找到 404 页面"查询的全部路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能创建一个最终路由来捕获所有 .. 并将用户反弹到 ASP.NET MVC 中的 404 视图?

Is it possible to create a final route that catches all .. and bounces the user to a 404 view in ASP.NET MVC?

注意:我不想在我的 IIS 设置中进行设置.

NOTE: I don't want to set this up in my IIS settings.

推荐答案

自己找到答案了.

Richard Dingwall 有一篇出色的文章,介绍了各种策略.我特别喜欢 FilterAttribute 解决方案.我不喜欢随意抛出异常,所以我会看看我是否可以改进:)

Richard Dingwall has an excellent post going through various strategies. I particularly like the FilterAttribute solution. I'm not a fan of throwing exceptions around willy nilly, so i'll see if i can improve on that :)

对于 global.asax,只需将此代码添加为您注册的最后一条路线:

For the global.asax, just add this code as your last route to register:

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

这篇关于如何为 ASP.NET MVC 处理“未找到 404 页面"查询的全部路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 00:33