Asp.net MVC4,defaultRedirect无法正常工作?

时间:2013-03-09 16:17:09

标签: asp.net-mvc-4

的Web.config

<customErrors mode="On" defaultRedirect="~/Error/HttpError"> // is not working
    <error redirect="~/Error/NotFound" statusCode="404" />
    <error redirect="~/Error/HttpError" statusCode="500" /> // also it is not working
</customErrors>

错误控制器

public class ErrorController : Controller
{
    //
    // GET: /Error/

    public ActionResult HttpError()
    {
        return Content("HttpError was called!");
        //return View("Error");
    }

    public ActionResult NotFound(string aspxerrorpath)
    {
        return View();
    }

    public ActionResult Index()
    {
        return RedirectToAction("Index", "Home");
    }
}

对于404错误,它也会调用NotFound操作,但对于其他错误,它从不会遇到HttpError方法。

public ActionResult ETest()
{
    throw new Exception("yahoo");
}

我运行上面的测试代码,它直接进入Error.cshtml页面。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

你在FilterConfigs.cs中有这个吗?

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
}

HandleError属性是处理默认错误所必需的。