Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-09-16
6
Medium Priority
?
298 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 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.

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 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);
            }
        }
    }
}


//web.config:

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

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

610 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