1

I am trying to redirect to error page in global.asax file of asp.net mvc5 when any exception occurs in application. After executing Response.Redirect line nothing happening.

It is not at all redirecting to errorpage which is available in the path ~\View\Shared\Error.cshtml

protected void Application_Error(object sender, EventArgs e)
{   
    Server.ClearError();
    Response.Clear();      
    HttpContext.Current.Response.Redirect("~\View\Shared\Error.cshtml");    
   //return;               
}

and in webconfig,

<system.web>
    <customErrors mode="On" defaultRedirect="~\View\Shared\Error.cshtml" />
</system.web>

I am not sure what's going wrong.

My error controller:

public class ErrorController : Controller
    {
        // GET: Error
        public ActionResult Error()
        {
            return View("Error");
        }

    }
Govind
  • 979
  • 4
  • 14
  • 45

2 Answers2

3

I wouldn't recommend to use Global.asax, unless you have some custom logic going on. I'd recommend using web.config. Just be aware that, since MVC uses routes instead of physical files you should use something like this in the web.config:

<httpErrors errorMode="Custom"> <remove statusCode="404"/> <error statusCode="404" path="/error/404" responseMode="ExecuteUrl"/> <httpErrors>

But in case you want to call some physical file (html, for example) you should use it in this way:

<httpErrors errorMode="Custom"> <remove statusCode="404"/> <error statusCode="404" path="/erro404.html" responseMode="File"/> <httpErrors>

Now, going back to the custom logic. If you really need to use Global.asax I'd recommend you to use something like this:

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

    HttpException httpException = exception as HttpException;

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

    if (httpException == null)
    {
        routeData.Values.Add("action", "Index");
    }
    else //It's an Http Exception, Let's handle it.
    {
        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 ErrorController();
    errorController.Execute(new RequestContext(
            new HttpContextWrapper(Context), routeData));
}
Davidson Sousa
  • 1,353
  • 1
  • 14
  • 34
  • i have tried your custom entire code, its hitting my controller but view is not redirected and the application error is hitting so many times. – Govind Jan 21 '15 at 15:36
  • Can you post the code of your controller? You should be calling the View from there. – Davidson Sousa Jan 22 '15 at 09:37
  • Hum... First, you shouldn't call the cshtml file directly in the error handling (in case you still are). Second, I wouldn't put the Controller and the Action with the same name as it *could* cause some routing problems. See that in my code the Controller is "Error" and the Action is "General". You could start by renaming your action and calling it to see if it works for you. – Davidson Sousa Jan 22 '15 at 14:33
1

You're doing it wrong.

~ returns physical paths to the root of the application, but obviously that my change.

Use Server.MapPath() method to check which path is the correct one in your case. Follow the following list:

  • Server.MapPath(".") returns the current physical directory of the file (e.g. aspx) being executed
  • Server.MapPath("..") returns the parent directory
  • Server.MapPath("~") returns the physical path to the root of the application
  • Server.MapPath("/") returns the physical path to the root of the domain name (is not necessarily the same as the root of the application)

Have a look at this brilliant answer too.

Community
  • 1
  • 1
U r s u s
  • 6,680
  • 12
  • 50
  • 88