Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 377181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:40:17+00:00 2026-05-12T14:40:17+00:00

I want to use Application_Error with my MVC project, but i can’t get it

  • 0

I want to use Application_Error with my MVC project, but i can’t get it to work. I add the following to my Global.asax file:

    protected void Application_Error(object sender, EventArgs e)
    {
        Exception objErr = Server.GetLastError().GetBaseException();
        Session["Test"] = "Message:" + objErr.Message.ToString();
    }

(The Session is only for tests. Im gonna use a database to log error, if i get this to work.)
Then i try to throw an exception from my HomeController and my Home/Index View, but it only triggers Debug.

    public ActionResult Index()
    {
        ViewData["Message"] = "Welcome to ASP.NET MVC!";
        throw (new Exception());
        return View();
    }

In my Webconfig file i set a defaulterror page but it doesn’t redirect to the view:

    <customErrors defaultRedirect="Home/Error">
        <error statusCode="403" redirect="NoAccess.htm" />
        <error statusCode="404" redirect="FileNotFound.htm" />
    </customErrors>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T14:40:17+00:00Added an answer on May 12, 2026 at 2:40 pm

    So firstly remember that global error handling should be a last resort, and controller classes have a specific error method for errors;

    protected virtual bool OnError(string actionName, 
        System.Reflection.MethodInfo methodInfo, Exception exception)
    

    Within this you can redirect to the standard shared error view;

    protected override bool OnError(string actionName, 
        System.Reflection.MethodInfo methodInfo, Exception exception)
    {
       RenderView("Error", exception);
       return false;
    }
    

    The problem you have in the global application error is that it has no concept of views or controllers, so if you want to redirect in there then you must use a known URL

    protected void Application_Error(object sender, EventArgs e)
    {
        Exception exception = Server.GetLastError();
        System.Diagnostics.Debug.WriteLine(exception);
        Response.Redirect("/Home/Error");
    }
    

    but you don’t need to do this. If you set the default error page in web.config then you don’t need that redirect

    <customErrors defaultRedirect="Home/Error" />
    

    However, unless you’ve added an error view to your Home controller that doesn’t exist, so add the following to the home controller

    public ActionResult Error()
    {
        return View();
    }
    

    Then (if you’re sensible) you’d put the error handling code in the Error() method, as that’s where all unhandled errors will end up.

    public ActionResult Error()
    {
        Exception exception = Server.GetLastError();
        System.Diagnostics.Debug.WriteLine(exception);
        return View();
    }
    

    And finally remember that by default you don’t see custom errors if you are connecting to localhost! So you need to change that behaviour

    <customErrors mode="On" defaultRedirect="/Home/Error" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use SimpleMembership in an MVC 3 website, but I'm getting the
I want to use pdo in my application, but before that I want to
I want to use the Web Browser control within an mono application, but when
I want to use an IFrame in my ASP.Net MVC application, yet I want
Ineed to write the Error Handler Helper classes for my asp.net mvc project.. can
I get the following error when trying to add a record: Serious application error.
Hi every body i am developing a MVC application, and i want to use
I am using Spring MVC and Hibernate. I want to use the OpenSessionInViewFilter to
We have a server control that we want to use in our .net MVC
Now I'm trying to work with System.Web.Routing. All is just fine, but I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.