03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

344 x CHAPTER 9 VALIDATING USER INPUT<br />

How It Works<br />

The mail-send<strong>in</strong>g part of this exercise is pretty similar to the demo page you created earlier. What’s<br />

different, however, is where the body text for the mail message comes from. Instead of hard-cod<strong>in</strong>g the<br />

body <strong>in</strong> the Code Beh<strong>in</strong>d of the ContactForm control, you moved the text to a separate file. This file<br />

<strong>in</strong> turn conta<strong>in</strong>s a few placeholders that are replaced at run time with the user’s details. To read <strong>in</strong> the<br />

entire file at once, you use the follow<strong>in</strong>g code:<br />

<strong>VB</strong>.<strong>NET</strong><br />

Dim fileName As Str<strong>in</strong>g = Server.MapPath("~/App_Data/ContactForm.txt")<br />

Dim mailBody As Str<strong>in</strong>g = File.ReadAllText(fileName)<br />

C#<br />

str<strong>in</strong>g fileName = Server.MapPath("~/App_Data/ContactForm.txt");<br />

str<strong>in</strong>g mailBody = File.ReadAllText(fileName);<br />

The first l<strong>in</strong>e uses Server.MapPath to translate a virtual path <strong>in</strong>to its physical counterpart. By us<strong>in</strong>g<br />

the virtual path, it’s easier to move your site to a different location because it doesn’t depend on any<br />

hard-coded paths. Server.MapPath("~/App_Data/ContactForm.txt") returns a physical path such as<br />

C:\Beg<strong>ASP</strong><strong>NET</strong>\Site\App_Data\ContactForm.txt. This path is then fed to the ReadAllText method<br />

of the File class, which opens the file <strong>and</strong> returns its contents, which are then assigned to the mail-<br />

Body variable.<br />

NOTE Read<strong>in</strong>g this file every time you need it isn’t very efficient. In Chapter 15,<br />

you see how to cache the contents of this fi le so you don’t have to read it on<br />

every request.<br />

The code then uses a number of calls to the Replace method of the Str<strong>in</strong>g class to replace the static<br />

placeholders <strong>in</strong> the message body with the details the user entered <strong>in</strong> the contact form. The return value<br />

of the Replace method—the new text with the replaced str<strong>in</strong>gs—is reassigned to the mailBody variable.<br />

After the f<strong>in</strong>al call to Replace, the mailBody no longer conta<strong>in</strong>s the placeholders, but the user’s<br />

details <strong>in</strong>stead:<br />

<strong>VB</strong>.<strong>NET</strong><br />

mailBody = mailBody.Replace("##Name##", Name.Text)<br />

...<br />

mailBody = mailBody.Replace("##Comments##", Comments.Text)<br />

C#<br />

mailBody = mailBody.Replace("##Name##", Name.Text);<br />

...<br />

mailBody = mailBody.Replace("##Comments##", Comments.Text);<br />

The Replace method is case sensitive, so if you f<strong>in</strong>d that some placeholders are not replaced correctly,<br />

make sure you used the same capitalization <strong>in</strong> the code <strong>and</strong> <strong>in</strong> the message body.<br />

The placeholders are wrapped <strong>in</strong> a pair of double hash symbols (##). The hash symbols are arbitrarily<br />

chosen, but help to identify the placeholders, m<strong>in</strong>imiz<strong>in</strong>g the risk that you accidentally replace some<br />

text that is supposed to be <strong>in</strong> the actual message.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!