c# - .Net Mvc : How to fire a error for Application_Error() manage them?

标签 c# .net asp.net asp.net-mvc asp.net-mvc-3

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

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() ,但对我来说如此直接调用异常似乎很难看,是否存在另一种方式?大家觉得怎么样?

最佳答案

因为默认情况下未授权不是错误!!! 只需将此方法添加到 global.asax

    protected void Application_EndRequest(object sender, EventArgs e) {
        if (Context.Response.StatusCode == 401 || Context.Response.StatusCode == 403) {
        // this is important, because the 401 is not an error by default!!!
            throw new HttpException(401, "You are not authorised");
        }
    }

关于c# - .Net Mvc : How to fire a error for Application_Error() manage them?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7501810/

相关文章:

c# - 与代表有什么关系?

c# - Bitshifting Int16 仅适用于文字

Visual Studio 2010 Express 中忽略了 C# 预处理器指令?

c# - 如何为一组具有几乎相同功能但具有不同参数和返回类型的类创建接口(interface)?

C# Winforms 应用程序发送电子邮件失败 : The remote name could not be resolved: 'smtp. gmail.com ;操作超时

.net - 如何开始开发 OPC Xi(.NET 3.0 WCF 服务)服务器(服务)

c# - 如何从派生类的实例中调用基类非默认构造函数?

c# - 如何知道继承的泛型类型的基类型?

java - Android应用程序,如何登录网站并显示信息?

asp.net - .NET MVC 3 (C#) 默认成员资格提供程序 - 如何设置?