cookieChoices = {};

Friday 20 September 2013

ASP Page Redirection Ways

ASP.NET provides a few ways to move to different pages.

Directing a user to another page or resource is a common aspect of a Web application. The user may initiate the transfer to another page any number of ways, including clicking a button or link or selecting a value in a drop-down list. ASP.NET provides a few ways to move to different pages. Here's a look at these options, along with commentary on when you should use which approach.

Client vs. server

A key aspect of the various ways to send a user to another page within an ASP.NET application is where the transfer occurs; that is, it is handled within the client browser or on the Web server. The following list outlines two options for controlling page navigation.
  • Response.Redirect: The Redirect method of the Response object provides a way to implement client-side redirection.
  • Server.Transfer: The Transfer method of the Server object performs redirection using the server and avoiding HTTP requests.
Let's take a closer look at each approach.
Response.Redirect The default behavior of the Response.Redirect method is execution of the current page halts, and the user is sent to the new URL. The Web server issues HTTP headers and sends a 302 response to the client browser; this instructs the client to make redirected calls to the target URL. The result is two server requests for each redirection: the original and redirected requests. The following C# snippet shows it in action:
Response.Redirect("http://www.news.com");
Now, the Redirect method has two signatures with the second format accepting another Boolean parameter that signals whether execution of the current page should terminate (the default behavior). We could tell the system to continue execution of the current page while redirecting the user to the News.com site with the following snippet:
Response.Redirect("http://www.news.com", false);
Server.Transfer The Server.Transfer method transfers the execution of a target URL to the server and halts execution of the current page. The result is only one request (as opposed to the two involved with the Response.Redirect method) since the server does not notify the client browser of the change. The experience can be a little disconcerting for users since the page address does not change in the browser. This C# snippet shows how you may use this method.
Server.Transfer("/default.aspx");
When using Server.Transfer, the target URL must be a virtual path on the same server since the Web server's worker process is used, so you can't use an address containing "http" or "https." It has three signatures, with a second variation allowing you to transfer control to an IHttpHandler and the third adding a second parameter to the first version; whereas, the second value is a Boolean signaling whether the current form's querystring and Form collections are preserved.
The PreviousPage property of the Page class provides code access to properties of the previous page in the browsing session, so Form and querystring variables are persisted between pages whereas they are not when using Response.Redirect.

Server.Execute

The Server.Execute method is a bit antiquated, as there are other ways to accomplish the task, but it basically allows you to execute a resource request without leaving the current page. It is not really used for redirection, but it is mentioned here only to avoid any confusion with the Server.Transfer method.
Server.Execute has five signatures, but the basic version accepts a path to a resource as the following snippet displays:
Server.Execute(ResourcePath);

Pros and cons of each approach

Redirecting a user to another Web resource is feasible in ASP.NET using one of the techniques discussed. However, you may be wondering why you would choose one approach over the other. The following list covers some of the advantages or disadvantages of Server.Transfer and Response.Redirect.
  • AJAX usage: The lack of browser interaction with the Server.Transfer method means it may break some AJAX and/or JavaScript functionality.
  • Bookmarking: Since Server.Transfer does its work on the server, the address within the client browser is not updated. The user sees the previous page's address while viewing a new page. Consequently, the user is unable to bookmark certain pages.
  • Page refreshes: There is an issue when a true value is used with the second parameter of the Server.Transfer method. When users refresh a page located via this approach, it can trigger an invalid ViewState error message. This can be alleviated by disabling the enableViewStateMac property on a page, but this isn't the best approach to security.
  • Performance: Response.Redirect introduces an extra call while making the roundtrip between client and server; since there is only one call with Server.Transfer, it offers better performance.
  • Scalability: The extra roundtrips associated with using Response.Redirect are often stated as a drawback with using it. However, I have seen it used in large applications without experiencing any performance issues.
  • Errors: While Server.Transfer can cause some user confusion as the URL displayed in the browser address bar, it can also lead to some confusion with error logging, as the page URL recorded during logging will display incorrectly.
  • Basic security: An interesting twist with using Server.Transfer is the ability to send data to another page without the user seeing it. This is enabled via the use of the QueryString, which is appended to the end of the target address. This new address will not show in the browser address bar with Server.Transfer, so it offers a simple layer of security. Of course, the Response.Redirect approach makes the QueryString visible to the user.

Usage depends on the situation

ASP.NET offers plenty of options when tackling a development task. The Response.Redirect and Server.Transfer methods provide two ways to redirect users to new URLs. Each method offers its own set of positive attributes, so deciding which one to use is really up to the developer's preference.

Example on Page Redirection:

  1. <%@ Page Language="C#" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <script runat="server">  
  6.     protected void Button1_Click(object sender, System.EventArgs e)  
  7.     {  
  8.         string url = "Image.aspx?";  
  9.         url += "ID=" + ListBox1.SelectedValue.ToString() + "&";  
  10.         url += "Name=" + Server.UrlEncode(ListBox1.SelectedItem.Text);  
  11.         Response.Redirect(url);  
  12.           
  13.     }  
  14. </script>  
  15.   
  16. <html xmlns="http://www.w3.org/1999/xhtml">  
  17. <head id="Head1" runat="server">  
  18.     <title>asp.net Redirect example: how to redirect a page to another page in asp.net</title>  
  19. </head>  
  20. <body>  
  21.     <form id="form1" runat="server">  
  22.     <div>  
  23.         <h2 style="color:Red">asp.net Redirect example</h2>  
  24.         <asp:Label   
  25.             ID="Label1"   
  26.             runat="server"   
  27.             Text="Select an Item for view Image"  
  28.             Font-Bold="true"  
  29.             ForeColor="SeaGreen"  
  30.             >  
  31.         </asp:Label>  
  32.         <br />  
  33.         <asp:ListBox   
  34.             ID="ListBox1"   
  35.             runat="server"   
  36.             ForeColor="AliceBlue"   
  37.             BackColor="Crimson"  
  38.             >  
  39.             <asp:ListItem Value="1">Avatar</asp:ListItem>  
  40.             <asp:ListItem Value="2">Flower</asp:ListItem>  
  41.             <asp:ListItem Value="3">Bird</asp:ListItem>  
  42.             <asp:ListItem Value="4">Elephant</asp:ListItem>  
  43.             <asp:ListItem Value="5">Fish</asp:ListItem>  
  44.             <asp:ListItem Value="6">Forest</asp:ListItem>  
  45.         </asp:ListBox>  
  46.         <asp:RequiredFieldValidator   
  47.             ID="RequiredFieldValidator1"  
  48.             runat="server"  
  49.             ControlToValidate="ListBox1"  
  50.             Text="*"  
  51.             >  
  52.         </asp:RequiredFieldValidator>  
  53.         <br />  
  54.         <asp:Button   
  55.             ID="Button1"   
  56.             runat="server"   
  57.             OnClick="Button1_Click"  
  58.             Text="Show Image"   
  59.             Font-Bold="true"   
  60.             ForeColor="HotPink"   
  61.             />  
  62.     </div>  
  63.     </form>  
  64. </body>  
  65. </html>  
Image.aspx 
  1. <%@ Page Language="C#" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <script runat="server">  
  6.     protected void Page_Load(object sender, System.EventArgs e) {  
  7.         Label1.Text = "Image ID: " + Request.QueryString["ID"];  
  8.         Label1.Text += "<br />Image Name: " + Request.QueryString["Name"];  
  9.   
  10.         string imageSource = "~/Images/" + Request.QueryString["ID"] + ".jpg";  
  11.         Image1.ImageUrl = imageSource;  
  12.         Image1.BorderWidth = 2;  
  13.         Image1.BorderColor = System.Drawing.Color.Green;  
  14.     }  
  15. </script>  
  16.   
  17. <html xmlns="http://www.w3.org/1999/xhtml">  
  18. <head id="Head1" runat="server">  
  19.     <title>asp.net Response.Redirect() example</title>  
  20. </head>  
  21. <body>  
  22.     <form id="form1" runat="server">  
  23.     <div>  
  24.         <h2 style="color:Navy">asp.net redirect example</h2>  
  25.         <asp:Label   
  26.             ID="Label1"   
  27.             runat="server"   
  28.             Font-Size="Large"   
  29.             ForeColor="CadetBlue"  
  30.             >  
  31.         </asp:Label>  
  32.         <br /><br />  
  33.         <asp:Image ID="Image1" runat="server" />  
  34.     </div>  
  35.     </form>  
  36. </body>  
  37. </html>  


 

 


No comments:

Post a Comment