【发布时间】:2010-09-23 13:26:06
【问题描述】:
是否可以在 ASP.NET MVC 中创建一个最终路由来捕获所有 .. 并将用户反弹到 404 视图?
注意:我不想在我的 IIS 设置中进行设置。
【问题讨论】:
-
查看我对“stackoverflow.com/questions/619895/…”的回答。
标签: asp.net-mvc routes http-status-code-404
是否可以在 ASP.NET MVC 中创建一个最终路由来捕获所有 .. 并将用户反弹到 404 视图?
注意:我不想在我的 IIS 设置中进行设置。
【问题讨论】:
标签: asp.net-mvc routes http-status-code-404
自己找到了答案。
Richard Dingwall 有一篇出色的帖子,介绍了各种策略。我特别喜欢 FilterAttribute 解决方案。我不喜欢随意抛出异常,所以我会看看我是否可以改进:)
对于 global.asax,只需将此代码添加为您注册的最后一条路线:
routes.MapRoute(
"404-PageNotFound",
"{*url}",
new { controller = "StaticContent", action = "PageNotFound" }
);
【讨论】:
{*url} 仍然没有解决这个问题。如果映射到路由的 URL 可用但控制器/操作本身没有找到怎么办。如果我浏览到 /Home/About123 丑陋的 asp.net 错误页面仍然会出现,因为路由没有到达 {*url} 但在我的主路由中处理。
这个问题最先出现,但更简单的答案出现在后面的问题中:
Routing for custom ASP.NET MVC 404 Error page
我通过创建一个 ErrorController 来进行错误处理 返回本文中的观点。我还必须添加“Catch All” 到 global.asax 中的路线。
如果不是,我看不到它会如何到达这些错误页面中的任何一个 在 Web.config..?我的 Web.config 必须指定:
customErrors mode="On" defaultRedirect="~/Error/Unknown"然后我还补充说:
error statusCode="404" redirect="~/Error/NotFound"希望这会有所帮助。
我现在喜欢这种方式,因为它很简单:
<customErrors mode="On" defaultRedirect="~/Error/" redirectMode="ResponseRedirect">
<error statusCode="404" redirect="~/Error/PageNotFound/" />
</customErrors>
【讨论】:
Config Error: The configuration section 'customErrors' cannot be read because it is missing a section declaration。
您还可以处理 Global.asax.cs 中的 NOT FOUND 错误,如下所示
protected void Application_Error(object sender, EventArgs e)
{
Exception lastErrorInfo = Server.GetLastError();
Exception errorInfo = null;
bool isNotFound = false;
if (lastErrorInfo != null)
{
errorInfo = lastErrorInfo.GetBaseException();
var error = errorInfo as HttpException;
if (error != null)
isNotFound = error.GetHttpCode() == (int)HttpStatusCode.NotFound;
}
if (isNotFound)
{
Server.ClearError();
Response.Redirect("~/Error/NotFound");// Do what you need to render in view
}
}
【讨论】:
在您的项目根 web.config 文件下添加此行。
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" />
<error statusCode="404" responseMode="ExecuteURL" path="/Test/PageNotFound" />
<remove statusCode="500" />
<error statusCode="500" responseMode="ExecuteURL" path="/Test/PageNotFound" />
</httpErrors>
<modules>
<remove name="FormsAuthentication" />
</modules>
【讨论】:
这可能是你使用时的问题
throw new HttpException(404);
当你想抓住它时,我不知道除了编辑你的网络配置之外还有什么其他方法。
【讨论】:
Exception。让你所有的控制器都派生自一个BaseController。在此 BaseController 中,您覆盖 OnException 并将 filterContext.Result 设置为重定向或查看结果。
创建包罗万象的路由的另一种方法是将Application_EndRequest 方法添加到您的MvcApplication per Marco's Better-Than-Unicorns MVC 404 Answer。
【讨论】:
在RouterConfig.cs 中添加以下代码:
routes.MapRoute(
name: "Error",
url: "{id}",
defaults: new
{
controller = "Error",
action = "PageNotFound"
});
【讨论】:
如果路由无法解析,则 MVC 框架将通过 404 错误.. 最好的方法是使用异常过滤器......创建一个自定义异常过滤器并像这样......
public class RouteNotFoundAttribute : FilterAttribute, IExceptionFilter {
public void OnException(ExceptionContext filterContext) {
filterContext.Result = new RedirectResult("~/Content/RouteNotFound.html");
}
}
【讨论】: