fiberOptics
fiberOptics

Reputation: 7165

How to handle thrown error inside Global.asax file in Asp.Net MVC

I have search and tried a lot of articles but still can't solve this problem. I have this code inside Global.asax file:

LogInClient("username", "password");

Because of updates happened in Windows Azure, all of my service (REST) can't be found (but this is another story). The web displays a Bad request error. What I want to happen is this, for any kind of error the site will edirect to the error page.

But I'm always redirected to this

http://127.0.0.1:81/Error?aspxerrorpath=/
https://127.0.0.1/Error?aspxerrorpath=/

I'm running my Asp.Net MVC project through Cloud project.

This is what I have done so far:

enter image description here

Web.Config

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

Global.asax file

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

enter image description here

I'm lost here. Please help.

Upvotes: 1

Views: 2613

Answers (1)

John
John

Reputation: 3546

You could have this in your global.asax:

    void Application_Error( object sender, EventArgs e )
    {
        Boolean errorRedirect = false;
        Boolean redirect404 = false;
        try
        {
            var exception = Server.GetLastError();
            var httpException = exception as HttpException;
            Response.Clear();
            Server.ClearError();
            var routeData = new RouteData();
            routeData.Values[ "controller" ] = "Errors";
            routeData.Values[ "action" ] = "General";
            routeData.Values[ "exception" ] = exception;
            Response.StatusCode = 500;
            if ( httpException != null )
            {
                Response.StatusCode = httpException.GetHttpCode();
                switch ( Response.StatusCode )
                {
                    case 403:
                        redirect404 = true;
                        break;
                    case 404:
                        redirect404 = true;
                        break;
                    default:
errorRedirect = true;
                        //todo: log errors in your log file here
                        break;
                }
            }

        }
        catch ( Exception ex )
        {
            errorRedirect = true;
        }

        if ( redirect404 )
        {
            //redirect to 404 page
            Response.Redirect( "~/404.htm" );
        }
        else if ( errorRedirect )
        {
            //redirect to error page
            Response.Redirect( "~/error.htm" );
        }
    }

also some errors can't be caught be the global.asax so then you also need to catch the aspx errors by putting the following in all aspx codebehinds or preferable in a single class that extends System.Web.UI.Page and then let all your codebehinds inherit from that class. The code to put there is as follows :

    protected override void OnError( EventArgs e )
    {
        try
        {
            //todo: log errors in your log files
        }
        catch ( Exception ex ) { }
        //redirect to error page
        Response.Redirect( "~/error.htm" );
    }

Upvotes: 1

Related Questions