08.01.2015 Views

Beginning Web Development, Silverlight, and ASP.NET AJAX

Beginning Web Development, Silverlight, and ASP.NET AJAX

Beginning Web Development, Silverlight, and ASP.NET AJAX

SHOW MORE
SHOW LESS

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

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

66<br />

CHAPTER 3 ■ WEB FORMS WITH <strong>ASP</strong>.<strong>NET</strong><br />

The Page Object<br />

When you are running an <strong>ASP</strong>.<strong>NET</strong> application, you have access to the Page object via the<br />

Page class. You can use this access to get valuable information such as the session, the<br />

cache, the HTTP request, <strong>and</strong> the HTTP response (which we looked at briefly earlier in<br />

the chapter), among others. Let’s take a look at some of these.<br />

The Request Object<br />

The Request object contains the values associated with the HTTP request that the browser<br />

issued to the server to invoke your page. Its most common use is to interrogate the request<br />

parameters. You’ve seen many URLs that look like this:<br />

http://server/page.aspxparam=value&param=value<br />

When <strong>ASP</strong>.<strong>NET</strong> sees a call like this, it creates an array of name/value pairs that you<br />

can access from your code. So, for example, to test for a parameter called Test1 <strong>and</strong> pass<br />

it to a string in your code, you would have C# that looks like this:<br />

if (String.IsNullOrEmpty(Request.Params["test1"])){<br />

strTest1 = Request.Params["test1"];<br />

}<br />

else<br />

{<br />

strTest = String.Empty;<br />

}<br />

You should always check to see if the parameter is present (non-null entry in the<br />

array) before you load it into an internal data store, such as strTest1 in the preceding<br />

case.<br />

You should also sanitize all input parameters after loading them to avoid security<br />

holes through events such as SQL injections.<br />

The Request object supports much more than managing the parameters, including<br />

cookies <strong>and</strong> certificates. See http://msdn2.microsoft.com/en-us/library/system.web.<br />

httprequest(vs.80).aspx for more information.<br />

The Response Object<br />

As you can imagine, if you can h<strong>and</strong>le requests, you can also h<strong>and</strong>le responses. This is<br />

done using the Response object, which can be used to write new output to the response<br />

buffer or to redirect the user to another page or site.

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

Saved successfully!

Ooh no, something went wrong!