问题描述
是否可以在 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 解决方案.我不喜欢在 willy nilly 周围抛出异常,所以我会看看我是否可以改进:)
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 页面未找到"查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!