"
ASP.NET (snapshot 2017) Microsoft documentation and samples

Part 8: Final Pages, Exception Handling, and Conclusion

by Joe Stagner

Tailspin Spyworks demonstrates how extraordinarily simple it is to create powerful, scalable applications for the .NET platform. It shows off how to use the great new features in ASP.NET 4 to build an online store, including shopping, checkout, and administration.

This tutorial series details all of the steps taken to build the Tailspin Spyworks sample application. Part 8 adds a contact page, about page, and exception handling. This is the conclusion of the series.

Contact Page (Sending email from ASP.NET)

Create a new page named ContactUs.aspx

Using the designer, create the following form taking special note to include the ToolkitScriptManager and the Editor control from the AjaxdControlToolkit. .

Double click on the “Submit” button to generate a click event handler in the code behind file and implement a method to send the contact information as an email.

[!code-csharpMain]

   1:  protected void ImageButton_Submit_Click(object sender, ImageClickEventArgs e)
   2:    {
   3:    try 
   4:      {
   5:      MailMessage mMailMessage = new MailMessage();
   6:      mMailMessage.From = new MailAddress(HttpUtility.HtmlEncode(TextBoxEmail.Text));
   7:      mMailMessage.To.Add(new MailAddress("Your Email Here")); 
   8:   
   9:      // mMailMessage.Bcc.Add(new MailAddress(bcc));
  10:      // mMailMessage.CC.Add(new MailAddress(cc));
  11:   
  12:     mMailMessage.Subject = "From:" + HttpUtility.HtmlEncode(TextBoxYourName.Text) + "-" + 
  13:                                      HttpUtility.HtmlEncode(TextBoxSubject.Text);
  14:     mMailMessage.Body = HttpUtility.HtmlEncode(EditorEmailMessageBody.Content); 
  15:     mMailMessage.IsBodyHtml = true;
  16:     mMailMessage.Priority = MailPriority.Normal;
  17:     SmtpClient mSmtpClient = new SmtpClient();
  18:     mSmtpClient.Send(mMailMessage);
  19:     LabelMessage.Text = "Thank You - Your Message was sent.";
  20:     }
  21:   catch (Exception exp)
  22:     {
  23:     throw new Exception("ERROR: Unable to Send Contact - " + exp.Message.ToString(), exp);
  24:     }
  25:  }

This code requires that your web.config file contain an entry in the configuration section that specifies the SMTP server to use for sending mail.

[!code-xmlMain]

   1:  <system.net>
   2:          <mailSettings>
   3:              <smtp>
   4:                  <network
   5:                       host="mail..com"
   6:                       port="25"
   7:                       userName=""
   8:                       password="" />
   9:              </smtp>
  10:          </mailSettings>
  11:      </system.net>

About Page

Create a page named AboutUs.aspx and add whatever content you like.

Global Exception Handler

Lastly, throughout the application we have thrown exceptions and there are unforeseen circumstances that cold also cause unhandled exceptions in our web application.

We never want an unhandled exception to be displayed to a web site visitor.

Apart from being a terrible user experience unhandled exceptions can also be a security problem.

To solve this problem we will implement a global exception handler.

To do this, open the Global.asax file and note the following pre-generated event handler.

[!code-csharpMain]

   1:  void Application_Error(object sender, EventArgs e)
   2:   {
   3:   // Code that runs when an unhandled error occurs
   4:   }

Add code to implement the Application_Error handler as follows.

[!code-csharpMain]

   1:  void Application_Error(object sender, EventArgs e)
   2:   {
   3:      Exception myEx =  Server.GetLastError();
   4:      String RedirectUrlString = "~/Error.aspx?InnerErr=" + 
   5:          myEx.InnerException.Message.ToString() + "&Err=" + myEx.Message.ToString();
   6:      Response.Redirect(RedirectUrlString);
   7:   }

Then add a page named Error.aspx to the solution and add this markup snippet.

[!code-aspxMain]

   1:  <center>
   2:    <div class="ContentHead">ERROR</div><br /><br />
   3:    <asp:Label ID="Label_ErrorFrom" runat="server" Text="Label"></asp:Label><br /><br />
   4:    <asp:Label ID="Label_ErrorMessage" runat="server" Text="Label"></asp:Label><br /><br />
   5:  </center>

Now in the Page_Load event handler extract the error messages from the Request Object.

[!code-csharpMain]

   1:  protected void Page_Load(object sender, EventArgs e)
   2:  {
   3:      Label_ErrorFrom.Text = Request["Err"].ToString();
   4:      Label_ErrorMessage.Text = Request["InnerErr"].ToString();
   5:  }

Conclusion

We’ve seen that that ASP.NET WebForms makes it easy to create a sophisticated website with database access, membership, AJAX, etc. pretty quickly.

Hopefully this tutorial has given you the tools you need to get started building your own ASP.NET WebForms applications!

Previous



Comments ( )
Link to this page: //www.vb-net.com/AspNet-DocAndSamples-2017/aspnet/web-forms/overview/older-versions-getting-started/tailspin-spyworks/tailspin-spyworks-part-8.htm
< THANKS ME>