本文介绍了净MVC框架:如何火的Application_Error一个错误()管理它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我管理所有应用程序错误回报在我的的Application_Error()中的的Global.asax 的:

I manage all app erros in my Application_Error() in Global.asax:

protected void Application_Error(object sender, EventArgs e)
{
    Exception exception = Server.GetLastError();

    Log.LogException(exception);

    Response.Clear();

    HttpException httpException = exception as HttpException;

    RouteData routeData = new RouteData();
    routeData.Values.Add("controller", "Erro");

    if (httpException == null)
    {
        routeData.Values.Add("action", "Index");
    }
    else //It's an Http Exception
    {
        switch (httpException.GetHttpCode())
        {
            case 404:
                //Page not found
                routeData.Values.Add("action", "HttpError404");
                break;
            case 500:
                //Server error
                routeData.Values.Add("action", "HttpError500");
                break;

            // Here you can handle Views to other error codes.
            // I choose a General error template
            default:
                routeData.Values.Add("action", "General");
                break;
        }
    }

    //Pass exception details to the target error View.
    routeData.Values.Add("error", exception);

    //Clear the error on server.
    Server.ClearError();

    //Avoid IIS7 getting in the middle
    Response.TrySkipIisCustomErrors = true;

    //Call target Controller and pass the routeData.
    IController errorController = new ErroController();
    errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
}

所以,我有一个自定义授权属性在我的应用程序,处理未经授权的请求,我想重定向到的Application_Error()操作的吧。

所以,我做的:

protected override void HandleUnauthorizedRequest(AuthorizationContext context)
{
    if (context.HttpContext.Request.IsAuthenticated)
    {
        throw new HttpException(403, "Forbidden Access.");
    }
    else
    {
        base.HandleUnauthorizedRequest(context);
    }
}

在这样的的Application_Error()被调用,但似乎丑我这样直接调用一个例外,存在另一种方式?你觉得是什么人?

In that way the Application_Error() is called, but it seems ugly to me to call a exception so directly, exist another way? What you think guys?

推荐答案

您code是罚款。默认情况下,如果你调用 base.HandleUnauthorizedRequest ,它抛出一个 401 例外,这是由窗体身份验证模块,你截获重定向到登录页面(这可能不是所期望的行为)。所以,你的做法是正确的。

Your code is fine. By default if you call the base.HandleUnauthorizedRequest it throws a 401 exception which is intercepted by the forms authentication module and you get redirected to the login page (which might not be the desired behavior). So your approach is correct.

另一种可能性是直接呈现相应的错误观点,如果你不想去通过的Application_Error

Another possibility is to directly render the corresponding error view if you don't want to go through the Application_Error:

protected override void HandleUnauthorizedRequest(AuthorizationContext context)
{
    if (context.HttpContext.Request.IsAuthenticated)
    {
        context.Result = new ViewResult
        {
            ViewName = "~/Views/Shared/Forbidden.cshtml"
        };
    }
    else
    {
        base.HandleUnauthorizedRequest(context);
    }
}

这篇关于净MVC框架:如何火的Application_Error一个错误()管理它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 06:55