1

所以基本上我有一个自定义授权属性,在某些时候它会引发异常。我想捕获异常并向用户显示消息。

这是控制器

[HandleError]
public class TestController : BaseController
{
    [CustomAuthorize("someperm, someperm2")]
    public ActionResult GamblerUser()
    {
        return View();
    }
}

这是 FilterConfig 中的代码

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

这是 global.asax.cs 中的代码

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    WebApiConfig.Register(GlobalConfiguration.Configuration);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
}

和 web.config

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

最后是视图本身

@model HandleErrorInfo

@{
    ViewBag.Title = "Error";
}

<h2>Sorry Error Occured</h2> <br />
<h2>@Model.Exception.Message</h2>

这是我在异常发生时收到的消息。

运行时错误描述:处理您的请求时发生异常。此外,在执行第一个异常的自定义错误页面时发生了另一个异常。请求已终止。

我知道我缺少一些非常基本的东西,但这只是让我头疼。我已经看了很多例子,我只是无法让这件事发挥作用。

4

1 回答 1

1

ViewBag 为空。我会在 html 中使用静态标题,或者像这样找到解决方法。

于 2013-11-12T15:02:12.890 回答