ASP code explanation for MVC page redirection

Source: Internet
Author: User
This article is mainly for you to introduce the ASP. NET MVC page redirection related data, with a certain reference value, interested in small partners can refer to

Page redirection in asp: Server.Execute ("m2.aspx"); After the server saves this page to the previous data, it turns the page to m2.aspx execution, and then returns to this page to continue execution. Merge the three results back to the browser.
The above is the server-side page steering so the browser does not appear the page change record (the displayed address will not change). Therefore, if the user refreshes this page, there may be some other unexpected situation. This type of page turns and can accomplish some other functions, such as accessing the server-side controls on the previous page.

First, Response.Redirect:

When the browser requests an ASPX page, it encounters the redirect (URL) method, which is equivalent to telling the browser that you need to access a page before the browser sends a request to the server to that page. Relocation is performed through the browser, which generates additional round trips between the server and the browser. In a situation where the network is not in good condition, two requests can greatly reduce the response speed of the application, and even occupy the extra bandwidth.

Summary, in the case of better network status, Redirect (URL) method is the most efficient!! Server.Transfer method and Server.Execute method are the most flexible!! The Server.Execute method consumes the most resources.

Two, jump page ASP three methods to provide comparison

1 Response.Redirect This jump page method jumps fast because it has to go 2 back and forth (2 times postback), but he can jump to any page, no site page limit (ie can be jumped from Yahoo to Sina), and cannot skip login protection. But slow speed is its biggest flaw! REDIRECT Jump mechanism: The first is to send an HTTP request to the client, the notification needs to jump to the new page, and then the client sends a jump request to the server side. It is important to note that all data stored in the internal space after the jump is lost, so the session needs to be used.
2 Server.Transfer fast, only need one postback, but .... He must be under the same site because it is a method of server. In addition, he can skip login protection. You can write a small program to try: Design a jump from page to page two, but to enter the page two need to login, form authentication, but if the jump statement using transfer, then it will not pop up the login page. The redirect request for this method occurs on the server side, so the URL of the browser is still reserved for the original page's address!
3 Sever.execute This method is mainly used on the page design, and he must jump to the same site under the page. This method is used when inserting the output of one page into another ASPX page, most of which is in a table, where a page is similar to a nested way in another page.

Three, how to choose the page redirection method

There are four ways to navigate the page in ASP. How do you choose one for your page?
• If you want to let users decide when to convert a page and which page to go to, the hyperlink works best.
• If you want to use a program to control the target of the transformation, but the timing of the conversion is determined by the user, use the Web server's hyperlink control to dynamically set its NavigateUrl property.
• If you want to connect a user to a resource on another server, use Response.Redirect.
• Use Response.Redirect to connect users to non-ASPX resources, such as HTML pages.
• When you need to leave the query string as part of the URL to the server, because the other 2 methods can not do 2 postback, the data first back to the server, using Response.Redirect.
• If you want to transfer the execution process to another ASPX page on the same Web server, You should use Server.Transfer instead of Response.Redirect, because Server.Transfer can avoid unnecessary network traffic for better performance and browsing results.
• If you want to capture the output of an ASPX page and then insert the result into a specific location on another ASPX page, use Server.Execute.
• If you want to ensure that the HTML output is legitimate, use Response.Redirect, and do not use the Server.Transfer or Server.Execute methods.
By the way, how to use the Chinese character in the query string using the redirect method, because the usual situation is garbled, because the URL does not support Chinese characters. This time you need to convert:
String Message =server.urlencode ("Welcome");
Convert first, use query string
Response.Redirect ("webform2.aspx?msg=" +message);
About Server.Execute
This page navigation is similar to a function call for an ASPX page, where the called page has access to the form data and query string collection that made the calling page, so the enableViewStateMac property of the page directive of the invoked pages is set to False.
By default, the output of the called page is appended to the current reply stream. But The Server.Execute method has an overloaded method that allows the output of the called page to be fetched through a TextWriter object (or its sub-object, such as a StringWriter object), rather than being appended directly to the output stream, so that the location of the called page output can be easily adjusted in the original page.

MVC page redirection is simple, mainly in the following ways:

1.response.redirect (); Method

Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.Mvc;namespace mvcdemo.controllers{[HandleError] public class Homecontroller:controller {public  actionresult Index ()  {   viewdata["Message" = "Welcome to ASP. mvc!";   Response.Redirect ("User/news");   return View ();  }  Public ActionResult About ()  {   return View ();}}  }

2.Return Redirect (); method

Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.Mvc;namespace mvcdemo.controllers{[HandleError] public class Homecontroller:controller {public  actionresult Index ()  {   viewdata["Message" = "Welcome to ASP. mvc!";   Return Redirect ("User/news");  }  Public ActionResult About ()  {   return View ();}}  }

3.Return redirecttoaction (); method

There are two overloads of this method

Redirecttoaction ("ActionName");//The method is written directly to the page, the premise must be in the change controller under the question page such as the previous index.aspx, and About.aspx

Redirecttoaction ("ActionName", "controllername")//The method directly written to ActionName and Controllername, the premise must be under the controller to change the page as in front of the index.aspx , and About.aspx

Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.Mvc;namespace mvcdemo.controllers{[HandleError] public class Homecontroller:controller {public  actionresult Index ()  {   viewdata["Message" = "Welcome to ASP. mvc!";   Return redirecttoaction ("News", "User");  Public ActionResult About ()  {   return View ();}}  }

"Recommended"

1. asp free Video Tutorial

2. asp Tutorials

3. Eon the ASP Basic video tutorial

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.