Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old May 8th, 2012, 11:53 AM
Authorized User
Points: 37, Level: 1
Points: 37, Level: 1 Points: 37, Level: 1 Points: 37, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2012
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Default Application_Error in Global.asax not firing

Hi Imar,

Firstly thank you for writing such a great book for novices, such as myself, to be able to understand.

Secondly, apologies if this has already been discussed, but I can't seem to find it on the forums.

My problem is with Chapter 19, p704. If I request DefaultTest.aspx, I get my custom error page but I don't receive an email. I have tried various things, changing customErrors mode to On/Off/RemoteOnly, without any joy. I have tried Googling this and the only answer I can see is people stating that Application_Error does not fire if IIS is set to integrated mode. Is this possibly the case? And if so, is there an alternate method or do I need to change my IIS setting to classic, and if so what are the disadvantages of doing this?

Thanks for any assistance,

Julian
Reply With Quote
  #2 (permalink)  
Old May 8th, 2012, 12:28 PM
Imar's Avatar
Wrox Author
Points: 66,761, Level: 100
Points: 66,761, Level: 100 Points: 66,761, Level: 100 Points: 66,761, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,147
Thanks: 65
Thanked 1,391 Times in 1,371 Posts
Default

Hi Julian,

Glad to hear you like the book.

Does it work correctly when you use the built-in web server that comes with VWD/VS?

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!
Reply With Quote
  #3 (permalink)  
Old May 8th, 2012, 01:14 PM
Authorized User
Points: 37, Level: 1
Points: 37, Level: 1 Points: 37, Level: 1 Points: 37, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2012
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi Imar,

I think I may have confused myself by Googling this. I haven't configured Visual Studio to use IIS, so I guess that can't be the problem. I believe it is using the built-in web server. When I run my site, it runs the "ASP.NET Development Server", I presume this is the built-in web server?

I have set a breakpoint in Application_Error in Global.aspx, but it doesn't seem to get fired.

Kind regards,

Julian
Reply With Quote
  #4 (permalink)  
Old May 8th, 2012, 01:17 PM
Imar's Avatar
Wrox Author
Points: 66,761, Level: 100
Points: 66,761, Level: 100 Points: 66,761, Level: 100 Points: 66,761, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,147
Thanks: 65
Thanked 1,391 Times in 1,371 Posts
Default

Hi Julian,

I figured that would be the case; that's why I asked ;-)

Can you post the code for your Global.asax and web.config file? Did you save Global.asax in the root of your site and named it Global.asax?

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!
Reply With Quote
  #5 (permalink)  
Old May 8th, 2012, 01:28 PM
Authorized User
Points: 37, Level: 1
Points: 37, Level: 1 Points: 37, Level: 1 Points: 37, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2012
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi Imar,

Yes, my Global.asax is in the root of my site and is named Global.asax.

Here is my Global.asax:

Code:
<%@ Application Language="C#" %>
<%@ Import Namespace="System.Net.Mail" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        // Code that runs on application startup

    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown

    }
        
    void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs
      if (AppConfiguration.SendMailOnError)
      {
        if (HttpContext.Current.Server.GetLastError() != null)
        {
          Exception myException = HttpContext.Current.Server.GetLastError().GetBaseException();
          string mailSubject = "Error in page " + Request.Url.ToString();
          string message = string.Empty;
          message += "<strong>Message</strong><br />" + myException.Message + "<br />";
          message += "<strong>StackTrace</strong><br />" + myException.StackTrace + "<br />";
          message += "<strong>Query String</strong><br />" + Request.QueryString.ToString() + "<br />";
          MailMessage myMessage = new MailMessage(AppConfiguration.FromAddress, AppConfiguration.ToAddress, mailSubject, message);
          myMessage.IsBodyHtml = true;
          SmtpClient mySmtpClient = new SmtpClient();
          mySmtpClient.Send(myMessage);
        }
      }
    }

    void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started

    }

    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.

    }

    public void Profile_OnMigrateAnonymous(object sender, ProfileMigrateEventArgs args)
    {
      ProfileCommon anonymousProfile = Profile.GetProfile(args.AnonymousID);

      //Copy over anonymous properties only
      //Example: Profile.AnonymousProperty = anonymousProfile.AnonymousProperty;

      ProfileManager.DeleteProfile(args.AnonymousID);
      AnonymousIdentificationModule.ClearAnonymousIdentifier();
      Membership.DeleteUser(args.AnonymousID, true);
    }
       
</script>
And here is my web.config:

Code:
<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <connectionStrings>
    <add name="PlanetWroxConnectionString1" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\PlanetWrox.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
    <add name="PlanetWroxEntities" connectionString="metadata=res://*/App_Code.PlanetWrox.csdl|res://*/App_Code.PlanetWrox.ssdl|res://*/App_Code.PlanetWrox.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\PlanetWrox.mdf;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient"/>
  </connectionStrings>
  <appSettings>
    <add key="FromAddress" value="judecoles@borgsys.co.uk" />
    <add key="FromName" value="Julian Coles" />
    <add key="ToAddress" value="judecoles@borgsys.co.uk" />
    <add key="ToName" value="Julian Coles" />
    <add key="SendMail" value="true" />
    <add key="ChartImageHandler" value="storage=file;timeout=20;dir=c:\TempImageFiles\;"/>
  </appSettings>
  <system.web>
    <trace mostRecent="true" enabled="false" requestLimit="100" pageOutput="false" localOnly="true"/>
    <anonymousIdentification enabled="true" cookieName="PlanetWroxAnonymous"/>
    <customErrors mode="On" defaultRedirect="~/Errors/OtherErrors.aspx" redirectMode="ResponseRewrite">
      <error statusCode="404" redirect="~/Errors/Error404.aspx"/>
    </customErrors>
    <profile>
      <properties>
        <add name="FirstName"/>
        <add name="LastName"/>
        <add name="DateOfBirth" type="System.DateTime"/>
        <add name="Bio"/>
        <add name="FavouriteGenres" type="System.Collections.Generic.List`1[System.Int32]"/>
        <add name="FavouriteTheme" defaultValue="Monochrome" allowAnonymous="true"/>
      </properties>
    </profile>
    <roleManager enabled="true"/>
    <membership>
      <providers>
        <clear/>
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web,&#xA;                 Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="LocalSqlServer" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" applicationName="/" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="1" passwordAttemptWindow="10" passwordStrengthRegularExpression=""/>
      </providers>
    </membership>
    <authentication mode="Forms">
      <forms timeout="1440"/>
    </authentication>
    <pages theme="Monochrome">
      <controls>
        <add tagPrefix="Wrox" tagName="Banner" src="~/Controls/Banner.ascx"/>
      </controls>
    </pages>
    <httpHandlers/>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="Microsoft.Build.Framework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
        <add assembly="System.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
        <add assembly="System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
        <add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Data.Entity.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
      </assemblies>
      <buildProviders>
        <add extension=".edmx" type="System.Data.Entity.Design.AspNet.EntityDesignerBuildProvider"/>
      </buildProviders>
    </compilation>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <handlers/>
  </system.webServer>
  <system.net>
    <mailSettings>
      <smtp deliveryMethod="Network" from="Julian Coles &lt;judecoles@borgsys.co.uk&gt;">
        <network userName="ac26571" password="gphu9ftvj" port="25" host="mail.authsmtp.com"/>
      </smtp>
    </mailSettings>
  </system.net>
  <location path="Management">
    <system.web>
      <authorization>
        <allow roles="Managers"/>
        <deny users="*"/>
      </authorization>
    </system.web>
  </location>
  <location path="MyProfile.aspx">
    <system.web>
      <authorization>
        <deny users="?"/>
      </authorization>
    </system.web>
  </location>
  <location path="ManagePhotoAlbum.aspx">
    <system.web>
      <authorization>
        <deny users="?"/>
      </authorization>
    </system.web>
  </location>
  <location path="NewPhotoAlbum.aspx">
    <system.web>
      <authorization>
        <deny users="?"/>
      </authorization>
    </system.web>
  </location>
</configuration>
Thanks for your help in this,

Julian
Reply With Quote
  #6 (permalink)  
Old May 8th, 2012, 01:34 PM
Imar's Avatar
Wrox Author
Points: 66,761, Level: 100
Points: 66,761, Level: 100 Points: 66,761, Level: 100 Points: 66,761, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,147
Thanks: 65
Thanked 1,391 Times in 1,371 Posts
Default

Could it be an issue with the AppConfiguration property? The property is called SendMailOnError but your config key is called SendMail:

<add key="SendMail" value="true" />

Have you tried debugging with a breakpoint in Application_Error?

Also, consider editing your message and removing the user name and password data.

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!
Reply With Quote
  #7 (permalink)  
Old May 8th, 2012, 02:03 PM
Authorized User
Points: 37, Level: 1
Points: 37, Level: 1 Points: 37, Level: 1 Points: 37, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2012
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Default Whoops!

Hi Imar,

Thanks so much for that, it now works. The reason it wasn't stopping at the breakpoint was because... I was starting it with Ctrl+F5, not F5, must remember that in future!

Thanks for the rapid response,

Kind regards,

Julian
Reply With Quote
  #8 (permalink)  
Old May 8th, 2012, 02:18 PM
Imar's Avatar
Wrox Author
Points: 66,761, Level: 100
Points: 66,761, Level: 100 Points: 66,761, Level: 100 Points: 66,761, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 16,147
Thanks: 65
Thanked 1,391 Times in 1,371 Posts
Default

Did you you make and change to the code? Or did you just think it didn't work because your breakpoint wasn't hit?

Also:
Quote:
consider editing your message and removing the user name and password data.
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!
Reply With Quote
  #9 (permalink)  
Old May 8th, 2012, 02:27 PM
Authorized User
Points: 37, Level: 1
Points: 37, Level: 1 Points: 37, Level: 1 Points: 37, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2012
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi Imar,

Once you had pointed out the error in my code and I changed it (changed key from "SendMail" to "SendMailOnError"), I then received the email. I thought "That's odd, how can it send the email without stopping at my breakpoint", and then of course I realised it must always have been firing Application_Error, but I'd forgotten to run in debug mode!

Cheers,

Julian
Reply With Quote
  #10 (permalink)  
Old May 12th, 2012, 10:46 AM
Authorized User
Points: 37, Level: 1
Points: 37, Level: 1 Points: 37, Level: 1 Points: 37, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Mar 2012
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
Default Following on...

Hi Imar,

I've only just had time to get back this. Continuing on, on p705 you state that 'When the setting is not defined or does not contain a value, each property throws an exception." I noticed that this hadn't happened when my key was set incorrectly. Changing back to the wrong key name and running in Debug mode the program paused at the exception and informed me that "ArgumentException was unhandled by user code", but when I run normally (not in debug), I don't get any exception thrown. Is this the correct behaviour or should I get a dialog box stating the exception?

Kind regards,

Julian
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to declare the global variable in global.asax? calyn_gately ASP.NET 3.5 Basics 0 August 6th, 2008 08:06 PM
what is global.asax? gaurav_jain2403 General .NET 1 February 3rd, 2006 01:40 PM
global.asax ashokparchuri ADO.NET 1 February 25th, 2005 07:40 AM
Application_Error in global.asax saleyoun@hotmail.com ASP.NET 1.x and 2.0 Application Design 2 April 15th, 2004 03:00 PM



All times are GMT -4. The time now is 01:20 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.