Asp.Net MVC (RC 5) 中的 404 Http 错误处理程序 [英] 404 Http error handler in Asp.Net MVC (RC 5)

查看:48
本文介绍了Asp.Net MVC (RC 5) 中的 404 Http 错误处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在框架不抛出异常 500 错误代码的情况下处理 404 错误?

How can I Handler 404 errors without the framework throwing an Exception 500 error code?

推荐答案

http://jason.whitehorn.ws/2008/06/17/Friendly-404-Errors-In-ASPNET-MVC.aspx 给出如下解释:

添加通配符路由规则作为最终规则:

Add a wildcard routing rule as your final rule:

routes.MapRoute("Error", 
                "{*url}", 
                new { controller = "Error", action = "Http404" });

任何与其他规则不匹配的请求都会被路由到错误控制器的 Http404 操作,您还需要对其进行配置:

Any request that doesn't match another rule gets routed to the Http404 action of the Error controller, which you also need to configure:

public ActionResult Http404(string url) {
    Response.StatusCode = 404;
    ViewData["url"] = url;
    return View();
}

这篇关于Asp.Net MVC (RC 5) 中的 404 Http 错误处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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