Need to redirect to error page if there is an occur occured in asp.net application

Posted on 2011-09-16
Medium Priority
Last Modified: 2012-05-12
Hi Guys....

I have a requirement where I need to redirect the user to error page if there is an error occurred in application.

Error may be any kind of thing, whether it is Session time out exception Or page not found exception.

For this, I used custom errors setting in web.config file. My custom error file is in Root folder of the application.

I tested the following  scenario in my application:

I set the session time out as 1 minute in web.config file. I ran my application. I waited for 1 minute and came back to application and clicked a button on my application. It is unable to go to my custom page. The problem I found is "custom page is content page for Master page in which a session is already initiated. " So, as already session is expired, it is unable to go to that page.

Then I tested using some public website, Google.com. It is redirected successfully.

So here my question is:

Do I need to give a separate URL  which are not in same project to redirect the user

Can't we have the file added in my project as a separate file instead of having it as a content page ?

I tried it by adding a page as a separate page. that too did not work.

Please solve this problem guys ?

Question by:rakhy_rakey
  • 3
  • 3
LVL 21

Assisted Solution

by:Dale Burrell
Dale Burrell earned 1200 total points
ID: 36552936
I'm not totally sure what the issue here is. Yes you definitely can use a page on your site as the custom error page. But you have to make sure that there is no error on the custom error page itself - which is what is sounds like may be happening.

Here is something to try, as your custom error page use a static html page on your site. Then when that works you can build a simple aspx page which will be your custom error page. But if there is an error in the custom error page of course it will not work. So if for example your custom error page uses a master page and there is an error in the master page, well for sure the custom error page will not work.


Author Comment

ID: 36554588
hi Dale....

Thanks for the reply. I tried it now again by adding a new aspx page for my root folder. It worked. But some how it did not work yesterday.

Any way thank you.

Now I have one more question regarding this.

I just want to write a code in Codebehind of my aspx page to dispolay the message according to the error.

Some blogs on the internet tells that we can use a method, I think we can override application_error in global.asax file to display the message.

What is the difference between these two approaches and which one should be used when ?

Thank you very much for the help...

LVL 21

Assisted Solution

by:Dale Burrell
Dale Burrell earned 1200 total points
ID: 36555612
There are many ways to handle errors - the best one is the one that handles them the way that you want :)

Here is something like what you are talking about http://www.codeproject.com/KB/aspnet/GlobalErrorHandler.aspx

However I would do something like the following myself http://stackoverflow.com/questions/1367752/how-to-show-a-custom-404-page-in-asp-net-without-redirect

It transfers control to the error page rather than using a client side redirect. And as you can see it accesses the error information which you can then pass to your page (store in session or pass in querystring) to display.

I used the code below.
//file located in app_code

using System;
using System.Web;
using System.Web.Configuration;

public class ErrorModule : IHttpModule
    public ErrorModule() { }

    public String ModuleName { get { return "Error Module"; } }

    public void Init(HttpApplication Context)
        // Listen for errors
        Context.Error += Context_Error;

    public void Dispose() { }
    private void Context_Error(Object Sender, EventArgs Args)
        HttpContext Context = HttpContext.Current;

        // This example only handle 404 errors
        // You could also add some similar logic for 500 internal server errors (logic errors) and do some logging
        HttpException Error = Context.Server.GetLastError() as HttpException;
        if (Error.GetHttpCode() == 404)
            // we can still use the web.config custom errors information to
            // decide whether to redirect
            CustomErrorsSection Config = (CustomErrorsSection)WebConfigurationManager.GetSection("system.web/customErrors");
            if (Config.Mode == CustomErrorsMode.On || (Config.Mode == CustomErrorsMode.RemoteOnly && Context.Request.Url.Host != "localhost"))
                // Set the response status code – if we set it here we don't need to also set it in the page itself
                // Context.Response.StatusCode = 404;

                // redirect to the 404 error page from the web.config
                if (Config.Errors["404"] != null) HttpContext.Current.Server.Transfer(Config.Errors["404"].Redirect);
                else HttpContext.Current.Server.Transfer(Config.DefaultRedirect);


      <add name="ErrorModule" type="ErrorModule, App_Code"/>

Open in new window

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!


Accepted Solution

rakhy_rakey earned 0 total points
ID: 36557607
Hi Dale..

Thank you for the suggestion.

Your code tells to redirect to the corresponding page depending on the error code. I think we don't need to do here explicitly, as we alrerady have simple way of doing it in web.config file.

Any way,

I have coded the solution for my problem in Application_Error method in Global.asax page. I collect the error details there and I save it in database.

Thank you very much.

Author Closing Comment

ID: 36585547
Dale gave me proper guidance to reach the solution. But it is not complete. I also tried to reach my solution by trying in many ways.
LVL 21

Expert Comment

by:Dale Burrell
ID: 36558292
rakhy_rakey - my code doesn't do a redirect - it does a transfer and there is a big difference between the 2. Doing a redirect (which is what web.config does) asks the browser to redirect to another page, but by then you've lost all the information about the error. My code does a server transfer, which transfer control from the current page to the error page, retaining the information about the error. Therefore using my code you wouldn't need to store the error details in the database - unless you do actually want a record of them.

Hope that makes sense.

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
If you are looking for an automated tool which can generate reports for Outlook emails and other items from PST file, then you can go for Kernel PST Reporter tool. The reports which are created by this tool are helpful to analyze and understand PST …

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question