Wrox Programmer Forums
|
BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0
This is the forum to discuss the Wrox book ASP.NET 2.0 Website Programming: Problem - Design - Solution by Marco Bellinaso; ISBN: 9780764584640
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 12th, 2008, 01:24 PM
Friend of Wrox
 
Join Date: Mar 2006
Posts: 310
Thanks: 0
Thanked 0 Times in 0 Posts
Default exceptions

Hello!

In one page I see this:

Code:
                    If Me.User.Identity.IsAuthenticated AndAlso _
                        (Me.User.IsInRole("Administrators") Or _
                        Me.User.IsInRole("Editors")) Then

                        dvwArticle.ChangeMode(DetailsViewMode.Edit)
                    Else
                        Throw New SecurityException( _
                            "You are not allowed to edit existent articles!")
                    End If


But, all we have the "custom errors="On" I suppose...

So, the user will never read "You are not allowed to edit existent articles!" !!
He will only read that one error occurred :( And he will think that the page have a lot of error's!

It's possible to show, in the error.aspx the exception message = "You are not allowed to edit existent articles!"

Thanks!
Max

 
Old February 12th, 2008, 03:30 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Yes, it's possible to show any properties of an exception you want in a custom error page.

It's not a straightforward as it may appear at first. That's because once the custom error page is displayed, the exception is considered "handled" and is immediately disposed of, so it's too late to get a reference to it at that point.

So, you need to somehow persist the exception before the custom error page is served, then get a reference to it from there.

 
Old October 15th, 2008, 10:02 PM
Registered User
 
Join Date: Aug 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi, I have the same problem, did you find a way to do it?
Thanks
Horacio

 
Old October 18th, 2008, 12:34 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

As I said before, you have to persist the exception before the custom error page is served. The best way to do that is to catch it in the Global.asax Application_Error event handler, which always fires before the custom error page.

So, in Global.asax, you could do something like this:

void Application_Error(Object sender, EventArgs e)
   {
      Exception ex = Server.GetLastError();
      Session["LastError"] = ex;
      Response.Redirect("/TBH_Web/Error.aspx", false);
      Server.ClearError();
   }

What this does is get the exception that caused the Application_Error event to fire. Then it stores the exception in the session. It then redirects to the custom error page, and clears the error so that it doesn't bubble up to the web.config customErrors handler.

Now, all you have to do is to retrieve the exception from the session in Error.aspx. You can add that code to the already existing Page_Load event handler:

protected void Page_Load(object sender, EventArgs e)
      {
         if (Session["LastError"] != null)
         {
            lblError.Visible = true;
            lblError.Text = (Session["LastError"] as Exception).Message;
            Session.Remove("LastError");
         }

         lbl404.Visible = (this.Request.QueryString["code"] != null && this.Request.QueryString["code"] == "404");
         lbl408.Visible = (this.Request.QueryString["code"] != null && this.Request.QueryString["code"] == "408");
         lbl505.Visible = (this.Request.QueryString["code"] != null && this.Request.QueryString["code"] == "505");
         lblError.Visible = (string.IsNullOrEmpty(this.Request.QueryString["code"]));
      }

Tah-dah! The error message should now be displayed in Error.aspx.

Hope this helps.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Nested Exceptions in c# JelfMaria VS.NET 2002/2003 5 June 28th, 2005 01:15 AM
When to use Exceptions kcraft Pro PHP 1 January 15th, 2005 02:37 PM
Web Exceptions dotnetprogrammer VS.NET 2002/2003 0 November 3rd, 2004 03:39 AM
throwing exceptions...!? jacob ASP.NET 1.0 and 1.1 Basics 3 October 9th, 2003 03:37 PM
Exceptions and errors nbnelson C# 0 August 5th, 2003 11:24 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.