Solved

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

Posted on 2011-09-16
6
275 Views
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 ?

Thanks,
Rakhy.
0
Comment
Question by:rakhy_rakey
  • 3
  • 3
6 Comments
 
LVL 21

Assisted Solution

by:Dale Burrell
Dale Burrell earned 300 total points
Comment Utility
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.

hth
0
 

Author Comment

by:rakhy_rakey
Comment Utility
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...

Rakhy,
0
 
LVL 21

Assisted Solution

by:Dale Burrell
Dale Burrell earned 300 total points
Comment Utility
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);
            }
        }
    }
}


//web.config:

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

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Accepted Solution

by:
rakhy_rakey earned 0 total points
Comment Utility
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.
Rakhy
0
 

Author Closing Comment

by:rakhy_rakey
Comment Utility
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.
0
 
LVL 21

Expert Comment

by:Dale Burrell
Comment Utility
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.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now