如何制作一条捕获所有路由来处理 ASP.NET MVC 的“404 页面未找到"查询? [英] How to make a catch all route to handle '404 page not found' queries for ASP.NET MVC?

查看:31
本文介绍了如何制作一条捕获所有路由来处理 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 解决方案.我不喜欢在 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 页面未找到"查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆