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.

Programmatic Redirection x 279<br />

How It Works<br />

When you use Response.Redirect, <strong>ASP</strong>.<strong>NET</strong> sends an <strong>in</strong>struction to the browser to tell it to fetch<br />

a new page. In technical terms, it sends a “302” HTTP status code to <strong>in</strong>dicate the page has moved<br />

temporarily. With this <strong>in</strong>struction it also sends the new URL, so the browser underst<strong>and</strong>s what page<br />

to fetch next. In this exercise, the new page was Target.aspx?Test=SomeValue, which conta<strong>in</strong>s<br />

both the page name <strong>and</strong> a query str<strong>in</strong>g. The Target.aspx page is then requested by the browser, the<br />

Page_Load event fires, <strong>and</strong> the query str<strong>in</strong>g is displayed on the label <strong>in</strong> the page. Because of this client<br />

redirect, the new page name <strong>and</strong> query str<strong>in</strong>g are fully exposed to the client. If you use Response<br />

.RedirectPermanent, <strong>ASP</strong>.<strong>NET</strong> sends out a “301 Moved Permanently” <strong>in</strong>struction. For some browsers,<br />

this means that if the orig<strong>in</strong>al page has previously been cached by the browser, you’re taken automatically<br />

to the new page if you request the orig<strong>in</strong>al page. Once the browser cache is cleared, a request is<br />

made aga<strong>in</strong> for the orig<strong>in</strong>al page. Other browsers may cont<strong>in</strong>ue to request the orig<strong>in</strong>al page. Search<br />

eng<strong>in</strong>es <strong>in</strong>terpret the 301 redirect as “don’t bother fetch<strong>in</strong>g this page aga<strong>in</strong>” <strong>and</strong> the page will no longer<br />

be <strong>in</strong>dexed.<br />

Redirects follow the same nam<strong>in</strong>g scheme for URLs as those used <strong>in</strong> server controls, so you can redirect<br />

to a page like ~/Default.aspx to redirect the user to the file Default.aspx <strong>in</strong> the website’s root.<br />

In contrast to Response.Redirect <strong>and</strong> Response.RedirectPermanent, there is also Server<br />

.Transfer, which redirects to another page at the server.<br />

Server-Side Redirects<br />

Server-side redirects are great if you want to send out a different page without modify<strong>in</strong>g the client’s<br />

address bar. This enables you to hide details of page names <strong>and</strong> query str<strong>in</strong>gs, which may lead to<br />

cleaner URLs from a user’s po<strong>in</strong>t of view. This is often used <strong>in</strong> so-called URL-rewrite scenarios that<br />

are used to create pretty URLs. For example, a user may request a page like this:<br />

http://www.doma<strong>in</strong>.com/Cars/Volvo/850/T5/<br />

Under the hood the server might transfer to:<br />

http://www.doma<strong>in</strong>.com/Cars/ShowCar.aspx?Make=643&Model=984&Type=7345<br />

Clearly, the first URL is a lot easier to underst<strong>and</strong> <strong>and</strong> type <strong>in</strong> a browser. It also enables a user to<br />

guess other URLs that match the same pattern. For example, there’s a fair chance you can request a<br />

page like this:<br />

http://www.doma<strong>in</strong>.com/Cars/Volvo/V70/R/<br />

<strong>and</strong> end up with the right page show<strong>in</strong>g you the Volvo V70 R.<br />

In addition to be<strong>in</strong>g easier to underst<strong>and</strong>, server-side transfers may also speed up your site a little.<br />

Instead of send<strong>in</strong>g a response to the browser to tell it to fetch a new page, which results <strong>in</strong> a new<br />

request for a page, you can transfer the user directly to a new page, sav<strong>in</strong>g you some network<br />

overhead.<br />

Server-side transfers are carried out with the Server object. Just as the Request <strong>and</strong> Response<br />

objects you saw earlier give you <strong>in</strong>formation about the request <strong>and</strong> the response, so does the Server

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

Saved successfully!

Ooh no, something went wrong!