Solved

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

Posted on 2011-09-16
6
279 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 21

Assisted Solution

by:Dale Burrell
Dale Burrell earned 300 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.

hth
0
 

Author Comment

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

Rakhy,
0
 
LVL 21

Assisted Solution

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


//web.config:

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

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Accepted Solution

by:
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.
Rakhy
0
 

Author Closing Comment

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

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 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