 |
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
|
|
|
|
|

May 8th, 2012, 11:53 AM
|
|
Authorized User
|
|
Join Date: Mar 2012
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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
|
|

May 8th, 2012, 12:28 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

May 8th, 2012, 01:14 PM
|
|
Authorized User
|
|
Join Date: Mar 2012
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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
|
|

May 8th, 2012, 01:17 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

May 8th, 2012, 01:28 PM
|
|
Authorized User
|
|
Join Date: Mar 2012
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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="data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\PlanetWrox.mdf;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient"/>
</connectionStrings>
<appSettings>
<add key="FromAddress" value="[email protected]" />
<add key="FromName" value="Julian Coles" />
<add key="ToAddress" value="[email protected]" />
<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,
 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 <[email protected]>">
<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
|
|

May 8th, 2012, 01:34 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

May 8th, 2012, 02:03 PM
|
|
Authorized User
|
|
Join Date: Mar 2012
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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
|
|

May 8th, 2012, 02:18 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

May 8th, 2012, 02:27 PM
|
|
Authorized User
|
|
Join Date: Mar 2012
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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
|
|

May 12th, 2012, 10:46 AM
|
|
Authorized User
|
|
Join Date: Mar 2012
Posts: 10
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
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
|
|
 |
|