首页 文章

有没有办法在ASP MVC 4中启用自定义错误?

提问于
浏览
1

我尝试了所有可能的方法,我在这里找到(How to make custom error pages work in ASP.NET MVC 4)或在这里:http://benfoster.io/blog/aspnet-mvc-custom-error-pages,这里:http://www.secretgeek.net/custom_errors_mvc但似乎没有人为我工作 .

我将它简化为minium,但在点击不存在的页面时仍然会出现丑陋的标准错误消息 .

我的Error.cshtml放在Views \ Shared文件夹中,它看起来像:

<div>
    <span>Error occurred</span>
</div>

尝试过:

@model System.Web.Mvc.HandleErrorInfo
@{
    ViewBag.Title = "Parking & Ticket Scan Information - Error";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div>
    <span>Error occurred</span>
</div>

我的web.config包含:

<customErrors mode="On" defaultRedirect="~/Error">
    <error redirect="~/Error/NotFound" statusCode="404" />
</customErrors>

但我也尝试过:

<customErrors mode="On" defaultRedirect="~/Error">
</customErrors>

我的ErrorController就像:

public class ErrorController : Controller
{
    [HandleError]
    public ViewResult Index()
    {
        return View();
    }
}

(有或没有[HandleError]属性,有和没有

public ViewResult NotFound()
{
    Response.StatusCode = 404;  //you may want to set this to 200
    return View("NotFound");
}

后来我在Global.asax中添加了路由:

routes.MapRoute(name:“PageNotFound”,url:“{* url}”,默认值:new {controller =“Error”,action =“Index”,id = UrlParameter.Optional}

似乎没有任何东西强制IIS显示自定义错误 . 我还尝试了什么?当然注释掉了filters.Add(new HandleErrorAttribute());将Error.cshtml移动到Views文件夹 .

我可以使用其他任何方法来启用它吗?

2 回答

  • 0

    你总是可以这样做

    public class HandleUnauthorizedAttribute : HandleErrorAttribute
    {
        public override void OnException(ExceptionContext filterContext)
        {
            base.OnException(filterContext);
    
            if (filterContext.Exception.GetType() != typeof (SecurityException)) return;
    
            var controllerName = (string) filterContext.RouteData.Values["controller"];
            var actionName = (string) filterContext.RouteData.Values["action"];
            var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
    
            filterContext.Result = new ViewResult
            {
                ViewName = "Unauthorized",
                ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
                TempData = filterContext.Controller.TempData
            };
            filterContext.ExceptionHandled = true;
            filterContext.HttpContext.Response.Clear();
            filterContext.HttpContext.Response.StatusCode = 403;
            filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
        }
    }
    

    此代码将所有SecurityExceptions和路由捕获到填充了HandleErrorInfo的自定义错误页面 .

    您可以随时为要捕获的任何异常创建单独的过滤器并路由到其他自定义错误页面 .

    然后在启动时注册过滤器

    public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleUnauthorizedAttribute());
            //more filters if you define them.
        }
    }
    

    并在/ Shared / Views中创建您的视图

  • 0

    所以我到目前为止找到的唯一可能的解决方案是添加一个对应于ErrorController inside View\Error 文件夹的操作方法名称的视图 . 所有其他指南中似乎都缺少这个微小的细节 .

    所以现在我有了ErrorContoller.cs:

    public class ErrorController : Controller
    {
        public ActionResult ErrorDefault()
        {
            return View();
        }
    }
    

    和Views \ Error文件夹中的ErrorDefault.cshtml:

    @{
        ViewBag.Title = "ErrorDefault";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    <h2>ErrorDefault</h2>
    

    和web.config:

    <customErrors mode="On" defaultRedirect="~/Error/ErrorDefault">
    </customErrors>
    

    如果您忘记将视图放在与控制器名称对应的子文件夹中,它将无法在MVC 4.0中运行 .

    不需要在RouteConfig中添加单独的路由 .

    我希望有人会觉得这很有用 . :)

相关问题