Solved

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

Posted on 2011-09-16
6
278 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
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

831 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