Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB 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 August 25th, 2011, 03:55 PM
Registered User
 
Join Date: Aug 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Understanding Global Error Handling

First off! Love the book. I wish you wrote a part 2, 3, 4, etc and the Pro version because your style of writing makes it easy to learn!

Now my concern or more likely confusion. Been working with the global error handling touched on page 666-.

First off you say, "You can map different types of
errors (server errors, page not found errors, security problems, and so forth) to different pages"

Great. I have this working almost. But here's what I don't understand. Lets say I "on purpose in this case' just type some gibberish random characters in my default.aspx code behind page load event to force an error. Knowing that normally building/debuggin this I would catch this. But lets ignore for now. So I run the website and of course it won't load and in this case it doesn't come back with the default redirect defined page "othererrors.aspx" page but a YSOD version. with this set to "on" or "remoteonly" I still keep away the specifics of the error from the end user but why no redirect to, if nothing else, the defaultredirect page. I see this YSOB--->

Code:
Runtime Error

Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine. 

Details: To enable the details of this specific error message to be viewable on remote machines, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "Off".


<!-- Web.Config Configuration File -->

<configuration>
    <system.web>
        <customErrors mode="Off"/>
    </system.web>
</configuration>

Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's <customErrors> configuration tag to point to a custom error page URL.


<!-- Web.Config Configuration File -->

<configuration>
    <system.web>
        <customErrors mode="RemoteOnly" defaultRedirect="mycustompage.htm"/>
    </system.web>
</configuration>

The Global.ascx still sends the email with all the good info (strack trace, etc)
Code:
Exception Type:	System.Web.HttpCompileException
Message:	d:\Inetpub\wwwrootwebsite\Default.aspx.cs(12): error CS1002: ; expected
So my question is shouldn't even an error like this go to the default redirect page? Here's where i am confused. There is no HTML error per-say so I couldn't use a default custom error right because what status code would this be? This isn't technically an HTML error at all, right So per your quotes at the top...does capturing a server error or compile error like this not work with the custom error page setup? The 404 redirect works fine.

I hope I made sense. Thanks again.

-------------------------
So I used this nice HTTP Web Debugging Proxy software IE add-on called Fiddler2. It shows when I load the default.asxp file that it is returning a result return code of 500! So then I would think the web.config custom error entry for error500 would come up instead of the .Net YSOD. More confused ;-)

Last edited by markmanxp; August 25th, 2011 at 04:07 PM.. Reason: Update
 
Old August 25th, 2011, 06:04 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yes, an error like this should trigger a 500 server error. Have you tried explicitly setting up an error page for 500 errors? Can you post the code for your web.config file? And does it work when you have a runtime exception? For example, try

throw new Exception("");

From Page_Load.

Also, make sure the error page itself does not throw an exception.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old August 26th, 2011, 09:13 AM
Registered User
 
Join Date: Aug 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply. Yes I had looked over my code like 12 times or so it seemed to try to see if I missed something in the web.config or missed setting up a custom page. But everything looked fine.

I tried accessing each error page directly, like you suggested. I had a 401, 403, and 500. The first two worked but the 500 custom error page threw an exception error. I was getting the ysod error "http status string is not valid". The stack showed line 12 of my CS file "Response.Status = "Internal server error." It didn't matter what status I tried here same error.

So what worked was found here http://stackoverflow.com/questions/4...alid-exception

So I changed

Response.Status = "Internal server error.";
Response.StatusCode = 500;

To

Response.StatusCode = 500;
Response.StatusDescription = "Internal server error.";

Now everything is working fine. Not sure why using the code syntax as you suggested in your book worked for the 401, and 404 pages but not the 500! In any case, I have changed them all now to use Response.StatusDescription instead and so far so good!
 
Old August 26th, 2011, 10:08 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

The error status in the book is prefixed with a number (e.g. 404 Not Found). Did you also try that with the 500 error? (E.g. 500 Internal Server Error). It seems that the Status property tries to separate the code and the description and then assign them to StatusCode and StatusDescription respectively as shown by this code:

Code:
 
public string Status
{
  get
  {
    return (this.StatusCode.ToString(NumberFormatInfo.InvariantInfo) + " " + this.StatusDescription);
  }
  set
  {
    int num = 200;
    string str = "OK";
    try
    {
      int index = value.IndexOf(' ');
      num = int.Parse(value.Substring(0, index), CultureInfo.InvariantCulture);
      str = value.Substring(index + 1);
    }
    catch
    {
      throw new HttpException(SR.GetString("Invalid_status_string"));
    }
    this.StatusCode = num;
    this.StatusDescription = str;
  }
}
So if you leave out the number, an Invalid_Status_String exception is raised.

Your solution works as well as you assign both parts separately. The Status property is just a shortcut to set both properties at once.

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old August 26th, 2011, 10:48 AM
Registered User
 
Join Date: Aug 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Bingo! If I understand you right that would be a correct assumption on your part. Yes looking closer to custom error page 500 - I had the Response.Status = “Internal Server Error”; which didn't have any number in it like the other two!

Makes perfect sense. In any case, now I know of TWO ways to go about this. Thanks Imar! I really appreciate your assistance.

Mark





Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception Handling Global Aspx Fed BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 11 February 22nd, 2010 06:34 AM
Ch 15 Try It Out, Handling Global Errors, p.583 VictorVictor BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 0 February 26th, 2006 10:50 PM
error handling Abhinav_jain_mca SQL Server 2000 1 August 24th, 2004 06:13 AM
Error Handling zaeem Classic ASP Basics 2 November 14th, 2003 10:51 AM
Error Handling zaeem Classic ASP Basics 2 November 14th, 2003 10:42 AM





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