简体   繁体   English

customerError处理asp.net mvc4

[英]customerError handling asp.net mvc4

So basically I have an Custom Authorize attribute and at some point it throwns an exception. 因此,基本上我有一个Custom Authorize属性,并在某个时候抛出异常。 I want to catch the exception and display the message to the user. 我想捕获异常并将消息显示给用户。

this is the contoller 这是控制器

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

this is the code in FilterConfig 这是FilterConfig中的代码

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

this is the code in global.asax.cs 这是global.asax.cs中的代码

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

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

and the web.config 和web.config

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

and finally the view itself 最后是视图本身

@model HandleErrorInfo

@{
    ViewBag.Title = "Error";
}

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

and this is the message I get whenever the exception occurs. 这是发生异常时我得到的消息。

Runtime Error Description: An exception occurred while processing your request. 运行时错误说明:处理您的请求时发生异常。 Additionally, another exception occurred while executing the custom error page for the first exception. 此外,在执行第一个异常的自定义错误页面时发生另一个异常。 The request has been terminated. 该请求已终止。

I know that I am missing something very basic but this thing is just giving me headache. 我知道我缺少一些非常基本的东西,但是这只是让我头痛。 I've look at tons of examples I just cant get this thing working. 我看了无数个例子,但我只是无法使它正常工作。

ViewBag is null. ViewBag为空。 I would use a static title in the html or find a way around it like this . 我会在html中使用静态标题或像这样找到解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM