Brief Introduction to ASP. net mvc page redirection, asp. netmvc

Source: Internet
Author: User

Brief Introduction to ASP. net mvc page redirection, asp. netmvc

Page redirection in asp.net: Server. execute ("m2.aspx"); after the server saves the data from this page to the previous one, It redirects the page to m2.aspx for execution, and then returns to this page for further execution. merge the three results and return them to the browser.
All of the above are server-side page redirection, so there is no page change record in the browser (the displayed address will not change ). Therefore, if you refresh this page, there may be some other unexpected situations. This type of page redirection can complete some other functions, such as accessing the server control on the previous page.

1. Response. Redirect:

When a browser requests An aspx page, the Redirect (url) method is used to tell the browser that you need to access a page first, so the browser then sends a request to the server. Relocation is performed through the browser, and an additional round-trip process is generated between the server and the browser. When the network condition is not good, the two requests will greatly reduce the response speed of the application and even occupy excessive bandwidth.

In summary, the Redirect (url) method is the most efficient when the network status is good !! The Server. Transfer Method and Server. Execute method are the most flexible !! The Server. Execute method occupies the most resources.

Ii. Comparison of three methods provided by the jump page asp.net

1 response. the redirect method does not jump quickly because it takes two rounds (two postback), but it can jump to any page, there is no website page restriction (that is, you can jump from Yahoo to Sina), and you cannot skip logon protection. However, slow speed is the biggest defect! Redirect jump mechanism: First, send an http request to the client. The notification needs to jump to a new page, and then the client sends a jump request to the server. It should be noted that after the jump, all data information stored in the internal space will be lost, so session is required.
2 server. transfer is fast, only one postback is required, .... It must be on the same site because it is a server method. In addition, he can skip logon protection. You can try to write a small program: design a jump from page 1 to page 2, but to enter page 2, You need to log on and perform form authentication. However, if the jump statement uses transfer, the logon page will not pop up. The redirection request of this method occurs on the server, so the url address of the browser still retains the address of the original page!
3 sever.exe cute this method is mainly used in the page design, and he must jump to the page under the same site. This method needs to be used to insert the output results of a page to another aspx page. Most of them are in the table, where a page is nested to another page.

3. How to Select page redirection Methods

There are four page navigation methods in asp.net. How do I select one for your page?
· If you want users to decide when to convert a page and which page to go to, hyperlink is the most suitable.
· If you want to use a program to control the conversion target, but the conversion time is determined by the user, use the HyperLink control of the Web server to dynamically set its NavigateUrl attribute.
· If you want to connect a user to resources on another server, use Response. Redirect.
· Use Response. Redirect to connect users to non-ASPX resources, such as HTML pages.
· When the query string needs to be retained to the server as part of the url, because the other two methods cannot achieve two postback operations, the data is first taken back to the server and Response. Redirect is used.
· If you want to transfer the execution process to another ASPX page of the same Web Server, you should use Server. transfer instead of Response. redirect, because Server. transfer can avoid unnecessary network communication to achieve better performance and browsing performance.
· If you want to capture the output result of An ASPX page and insert the result to a specific location of another ASPX page, use Server. Execute.
· To ensure that the HTML output is valid, use Response. Redirect instead of Server. Transfer or Server. Execute.
By the way, how to use the redirect method to use Chinese Characters in query strings is often garbled because URLs do not support Chinese characters. In this case, the conversion is required:
String message = server. urlencode ("welcome ");
Convert the query string first.
Response. redirect ("webform2.aspx? Msg = "+ message );
About Server. Execute
This page navigation method is similar to calling the callback function for the ASPX page. The called page can access the form data and query string set of the calling page, therefore, set the EnableViewStateMac attribute of the called Page command to False.
By default, the output of the called page is appended to the current response stream. However, Server. the Execute method has an overloaded method that allows a TextWriter object (or its sub-object, such as the StringWriter object) to obtain the output of the called page, rather than directly appending it to the output stream, on the original page, you can easily adjust the location of the output result of the called page.

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

 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. net 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. net mvc! "; Return Redirect (" User/News ");} public ActionResult About () {return View ();}}}

3. Return RedirectToAction (); Method

This method has two kinds of Overload

RedirectToAction ("ActionName"); // This method is used to directly write data to the page, provided that the page is changed to the Controller, such as Index. aspx, and About. aspx.

RedirectToAction ("ActionName", "ControllerName") // This method is used to write the ActionName and ControllerName directly, provided that you modify the Controller's following page, such as 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. net mvc! "; Return RedirectToAction (" News "," User ") ;}public ActionResult About () {return View ();}}}

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.