0

I develop an ASP.NET MVC application. I would like to redirect user to error page if an error is occured. But if the error occured when the application is initializing I can not redirect user because "Response is not available in this context". Руку is my code:

    protected void Application_Start()
    {
        //...

        throw new Exception("qwerty");
    }

    protected void Application_Error(object sender, EventArgs e)
    {      
        // Here I have got an HttpException with message 
        // "Response is not available in this context"
        HttpContext.Current.Response.RedirectToRoute("Default");                           
    }

Is there way to redirect user to error page if the error occured when Application was initializing?

Pupkin
  • 1,211
  • 3
  • 13
  • 30
  • `if the error occured when Application was initializing` Do you mean 'prior to when it starts servicing web requests'? – mjwills Jun 19 '17 at 13:26
  • @mjwills I mean "error occured `in Application_Start` method". English is not my native language and I can make mistakes in phrases – Pupkin Jun 19 '17 at 13:29
  • Possible duplicate of [How to make custom error pages work in ASP.NET MVC 4](https://stackoverflow.com/questions/13905164/how-to-make-custom-error-pages-work-in-asp-net-mvc-4) – Alexander Jun 19 '17 at 13:50
  • @mjwills when I wrap this line in a try/catch block Application doesn't work. in catch block I catch HttpException – Pupkin Jun 19 '17 at 13:57
  • So `try { HttpContext.Current.Response.RedirectToRoute("Default"); } catch {}` didn't work? – mjwills Jun 19 '17 at 14:04
  • This would not work because if your application can't start then it can't respond to requests. You could use IIS error pages possibly, depending on your specific situation. – Dillon Jun 19 '17 at 14:36
  • Maybe because the application did not starts? So the routes are not defined... – marcoaoteixeira Jun 19 '17 at 14:43
  • @Pupkin Can you please update your post with your current code? – mjwills Jun 19 '17 at 23:12

0 Answers0