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

查看:434
本文介绍了如何捕获所有路径以处理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天全站免登陆