Wrox Programmer Forums
|
BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6
This is the forum to discuss the Wrox book ASP.NET 2.0 Instant Results by Imar Spaanjaars, Paul Wilton, Shawn Livermore; ISBN: 9780471749516
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 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 16th, 2010, 12:58 AM
Fed Fed is offline
Authorized User
 
Join Date: Oct 2009
Posts: 55
Thanks: 26
Thanked 0 Times in 0 Posts
Default Exception Handling Global Aspx

Hi

I implemented the global.aspx as defined in the 3.5 book. I basically added the code in the book to the file share app and it builds fine but when I force the 404 error to test it I get the exception "The SMTP Host was not found".

Because the file share app builds the SMTP host using structures as shown in the code below by taking the values from the web.config file.

WebConfig
Code:
<appSettings>
    <add key="EmailFrom" value="[email protected]"/>
    <add key="EmailSubject" value="File Ready for Download"/>
    <add key="SmtpServer" value="mail.infodomini.com"/>
    <add key="MailUser" value="[email protected]"/>
    <add key="MailPassword" value="tebedu01"/>
    <add key="MailPort" value="25"/>
    <add key="EmailFormatSelected" value="Text"/>
    <add key="PageTitle" value="Info Domini File Share"/>
    <add key="ShareLocalFolderPath" value="C:\FileShare\Release\FileStorage\"/>
    <add key="httpDownloadPath" value="http://localhost/ContentFiles/"/>
    <!--
      <add key="CurrentTheme" value="CleanBlue" />
      -->
    <add key="CurrentTheme" value="CleanRed"/>
  </appSettings>
Builds the SMTP HOST here
Code:
Imports Microsoft.VisualBasic
Imports System.Net.Mail
Imports System.Web.Configuration
Imports System.Configuration
Imports System.Net.Mail.SmtpClient


Public Class Utilities


    ''' <Summary>
    ''' MailSettings is a struct used to define the mail server information
    ''' </Summary>
    Public Structure MailSettings
        Public MailServer As String
        Public MailPort As Integer
        Public MailFrom As String
        Public MailUser As String
        Public MailPassword As String
    End Structure

    ''' <Summary>
    ''' GetSmtpSettings is used to retrieve the web.config file values and set them to the struct instance properties
    ''' </Summary>
    Private Shared Function GetSmtpSettings() As MailSettings

        Dim SmtpSettings As MailSettings
        SmtpSettings.MailServer = WebConfigurationManager.AppSettings("SmtpServer")
        SmtpSettings.MailFrom = WebConfigurationManager.AppSettings("EmailFrom")
        SmtpSettings.MailPort = CInt(WebConfigurationManager.AppSettings("MailPort"))
        SmtpSettings.MailUser = WebConfigurationManager.AppSettings("MailUser")
        SmtpSettings.MailPassword = WebConfigurationManager.AppSettings("MailPassword")
        Return SmtpSettings

    End Function

    ''' <summary>
    ''' GetCredentials is used to set the network credential values for the mail server reference
    ''' </summary>
    Private Shared Function GetCredentials(ByVal SmtpSettings As MailSettings) As System.Net.NetworkCredential
        Dim Wuser As String = SmtpSettings.MailUser
        Dim Wpassword As String = SmtpSettings.MailPassword
        Dim myCredentials As New System.Net.NetworkCredential(Wuser, Wpassword)
        Return myCredentials
    End Function

    ''' <summary>
    ''' SendEmail is used to send an email with the established settings and parameters
    ''' </summary>
    Public Shared Sub SendEmail(ByVal MsgTo As String, ByVal MsgFrom As String, ByVal MsgSubject As String, ByVal MsgText As String)
        Dim SmtpSettings As MailSettings
        SmtpSettings = GetSmtpSettings()
        Dim SmtpCl As New SmtpClient(SmtpSettings.MailServer, SmtpSettings.MailPort)
        SmtpCl.Credentials = GetCredentials(SmtpSettings)
        Dim MailMsg As New MailMessage(MsgFrom, MsgTo)
        MailMsg.Subject = MsgSubject
        MailMsg.Body = MsgText
        SmtpCl.Send(MailMsg)
    End Sub
End Class
The last SUB above is the equivalent of your ASP code in the Global.aspx example in the 3.5 book and I wanted to know if there is a way for me to replicate it without major changes?

Thanks,

Fed
 
Old February 16th, 2010, 03:43 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 Fed,

I don't recognize any of this from my book. I also don't see a direct problem with this code.

What happens when you debug? Do you see the code assigning an SMTP Server?

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 February 16th, 2010, 11:46 AM
Fed Fed is offline
Authorized User
 
Join Date: Oct 2009
Posts: 55
Thanks: 26
Thanked 0 Times in 0 Posts
Default

You are correct on all counts Imar, the code below works perfectly and it isn't from your 3.5 but it is from the file app share in 2.0.

This is your code from 3.5

Global .aspx
Code:
<%@ Application Language="VB" %>
<%@ Import Namespace="System.Net.Mail" %>

<script runat="server">

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs on application startup
    End Sub
    
    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs on application shutdown
    End Sub
        
    Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        ' Code that runs when an unhandled error occurs
        
        If HttpContext.Current.Server.GetLastError() IsNot Nothing Then
            Dim myException As Exception = _
                             HttpContext.Current.Server.GetLastError().GetBaseException()
            Dim mailSubject As String = "Error in page " & Request.Url.ToString()
            Dim message As String = 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 />"
            Dim mySmtpClient As SmtpClient = New SmtpClient()
            Dim myMessage As MailMessage = New MailMessage("[email protected]", _
                                "[email protected]", mailSubject, message)
            myMessage.IsBodyHtml = True
            mySmtpClient.Send(myMessage)
        End If

    End Sub
And this code gets an exception at mySmtpClient.Send(myMessage) of "The SMTP Host was not specified"

So no, the code does not assign an SMTP Server and I believe it is because of the way it is structured in the file share web.config.

Did I clarify? I know it sounds convoluted to me too. And I wanterd to know what is the best way to modify the Global.aspx to work with the file share app.

And that is why I included the code from the file share app in the previous post to try and explain the problem better. I now included the Global.aspx code from your 3.5 book that I am trying to incorporate in the file share app. And I think it doesn't work as is because of the way the SMTP settings are defined in the web.config of the file share app and want to know what is the best way to resolve so it works.

Thanks,

Fed

Last edited by Fed; February 16th, 2010 at 11:54 AM..
 
Old February 16th, 2010, 11:55 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Take a look at Chapter 9; it explains how to properly configure the web.config file with e-mail settings such as a mail server.

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!
The Following User Says Thank You to Imar For This Useful Post:
Fed (February 16th, 2010)
 
Old February 16th, 2010, 05:46 PM
Fed Fed is offline
Authorized User
 
Join Date: Oct 2009
Posts: 55
Thanks: 26
Thanked 0 Times in 0 Posts
Default

Believe it or not, I thought about that but figured that it was too easy to be true and that it would have interfered with the existing smtp stuff that was already there. I was wrong on both counts it works fine. So much for thinking...

I finally ironed out all the little things so I'm reay to put it live but for one other issue. He uses Login.aspx instead of default or index. The hosting tech support said that Windows based only recognizes default or index and that I either rename it or write a script. I tried to just rename login to default and it works except for when it tries to execute this;

Code:
<asp:LoginStatus ID="LoginStatus1" runat="server" LogoutAction="RedirectToLoginPage"
The renaming of the login.aspx to default.aspx causes the above to go in an endless loop.

Is there something else I can do or am I stuck with writing a script? And if script is the case do you have one lying around? :)

Thanks

PS

Just dawned on me that I could have both a login.aspx and a copy of it and call it default.aspx, what do you think of that?

Last edited by Fed; February 16th, 2010 at 05:58 PM..
 
Old February 17th, 2010, 03:32 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You could do that, but you could also try setting the loginUrl of the forms authentication element in web.config.

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!
The Following User Says Thank You to Imar For This Useful Post:
Fed (February 18th, 2010)
 
Old February 18th, 2010, 05:52 PM
Fed Fed is offline
Authorized User
 
Join Date: Oct 2009
Posts: 55
Thanks: 26
Thanked 0 Times in 0 Posts
Default

Wouldn't that still need to have both a default.aspx and a login.aspx? I looked it up on the net but did not find a clear example that I thought applied to me, do you happen to have one?

Recently my toolbox in VS 2008 went crazy and started duplicating the controls 4 or 5 times and after inumerable searches on forums and whatnot no solution presented itself. I got tired of waiting minutes for the toolbox to initialize and tired of looking for controls in a duplicate mess that I decided to re-install VS 2008. Now I realize that I had some hotfixes which I obviously no longer remember and something about SP1 is nagging me. But I looked at my newly reinstalled version and it says Version 3.5 SP1 but I'm having a strange problem that when I F5 an aspx IE pops up a blank page and after I close that blank page the aspx page comes up. I'm pretty sure that was a hotfix I applied but don't remember which... Any idea? Suggestion?

Thanks,

Fed
 
Old February 18th, 2010, 10:13 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Quote:
Wouldn't that still need to have both a default.aspx and a login.aspx?
Then I am not sure I understand what you're trying to accomplish. Can you elaborate?

Don't know what patch you're referring to. Maybe Google knows more?

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 February 19th, 2010, 10:48 AM
Fed Fed is offline
Authorized User
 
Join Date: Oct 2009
Posts: 55
Thanks: 26
Thanked 0 Times in 0 Posts
Default

Google came through...

As for the other question, when you go to a URL without a file named at the end, the server looks for a default file and displays that automatically. Just as if you had typed in that file name in the URL:

http://webdesign.about.com/index.htm

There are Three Default Page Names

•index.html (aspx)
•index.htm (aspx)
•default.htm (aspx)

The file share app uses login.aspx and when I uploade it to my hosting the tech said that the windows server needed the default page in order to work.

So I made a copy of login.aspx and renamed it default.aspx which is working when I'm in development mode with F5 but it is now bombing out in IIS and I'm looking into it. Also your Error routine is coming through very well and this is the email I'm getting when I'm trying to run it in IIS;

Code:
Message
C:\FileShare\Release\Login.aspx(1): error BC30456: 'InitializeCulture' is not a member of 'ASP.login_aspx'.
StackTrace
at System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath) at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile) at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile) at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean noAssert) at System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp, Boolean noAssert) at System.Web.UI.PageHandlerFactory.GetHandlerHelper(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath) at System.Web.UI.PageHandlerFactory.System.Web.IHttpHandlerFactory2.GetHandler(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath) at System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig) at System.Web.HttpApplication.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Query String
What does it mean?

Thanks,

Fed
 
Old February 19th, 2010, 03:29 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I am not 100% sure as you're not posting your code, but it looks like your markup file and Code Behind don't line up...

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!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Exception Handling Fed BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 4 February 16th, 2010 05:49 PM
Exception handling codehelp C# 2008 aka C# 3.0 4 August 27th, 2009 07:46 AM
Exception Handling NewTitle2007 BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 6 August 8th, 2007 04:03 AM





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