ASP. NET MVC Page Redirection Brief Introduction

  • 2021-09-11 19:58:08
  • OfStack

Page redirection in asp. net: Server. Execute ("m2. aspx"); Server save the page before the data, so that the page turned to m2.aspx execution, and then return to the page to continue to execute.
All of the above are server-side page shifts so the browser does not have a page change record (the displayed address does not change). Therefore, if the user refreshes this page, there may be 1 other unexpected situation. This kind of page turning can accomplish 1 other function, such as accessing the server-side control in the previous 1 page.

1. Response.Redirect:

When the browser requests the aspx page, it encounters the Redirect (url) method, which is equivalent to telling the browser that you need to visit a certain page first, and then the browser sends a request to this page to the server. Relocation is performed through the browser, creating additional round trips between the server and the browser. When the network condition is not very good, two requests will greatly reduce the response speed of the application, and even occupy redundant bandwidth.

To sum up, Redirect (url) method is the most efficient when the network state is good! ! The Server. Transfer and Server. Execute methods are the most flexible! ! The Server. Execute method consumes the most resources.

2. Jump page asp. Comparison of three methods provided by net

1 response. redirect This method of jumping pages is not fast, because it has to go back and forth 2 times (postback twice), but it can jump to any page without site page restriction (that is, it can jump from Yahoo to Sina), and it can't skip login protection at the same time. But slow speed is its biggest defect! redirect jump mechanism: First, send an http request to the client, notify the need to jump to a new page, and then the client sends a jump request to the server. It should be noted that all data information stored in the internal space will be lost after jumping, so session is needed.
2 server. transfer is fast and only needs postback once, 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 1 to Page 2, but to enter Page 2, you need to log in and form authentication, but if the jump statement uses transfer, the login page will not pop up. The redirect request for this method occurs on the server side, so the browser's url address remains the address of the original page!
3 sever. execute This method is mainly used in page design, and it must jump to the page under the same site. This method is used when the output result of one page needs to be inserted into another aspx page, most of which is in the table, and a certain one page exists in another one page in a nested way.

3. How to choose a page redirection method

In asp. net, there are 4 page jump navigation methods. How to choose one for your page?
If you want users to decide when to switch pages and which page to go to, hyperlinks are most suitable.
If you want to use a program to control the target of conversion, but the timing of conversion is decided by the user, use the HyperLink control of Web server to dynamically set its NavigateUrl property.
If you want to connect users to resources on another server, use Response. Redirect.
Use Response. Redirect to connect users to resources other than ASPX, such as HTML pages.
When it is necessary to keep the query string as url1 part and send it to the server, because the other two methods can't do postback twice, bring the data back to the server first and use Response. Redirect.
If you want to transfer the execution process to another ASPX page with the 1Web server, you should use Server. Transfer instead of Response. Redirect, because Server. Transfer can avoid unnecessary network communication and achieve better performance and browsing effect.
· Use Server. Execute if you want to capture the output from one ASPX page and then insert the results into a specific location on another ASPX page.
To ensure that the HTML output is legal, use Response. Redirect instead of Server. Transfer or Server. Execute methods.
By the way, how to use the redirect method to use Chinese characters in the query string, because garbled characters often appear, because url does not support Chinese characters. At this time, you need to convert:
string message = server. urlencode ("Welcome");
Conversion first, using the query string
response.redirect("webform2.aspx?msg="+message);
About Server. Execute
This page navigation mode is similar to a function call for ASPX page. The called page can access the form data and query string collection of the calling page, so the EnableViewStateMac attribute of the Page instruction of the called page should be set to False.
By default, the output of the called page is appended to the current reply flow. However, the Server. Execute method has an overloaded method that allows the output of the called page to be fetched through an TextWriter object (or its child objects, such as the StringWriter object) rather than appended directly to the output stream, making it easy to reposition the output of the called page in the original page.

MVC page redirection is very simple and mainly takes 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

There are two overloads for this method

RedirectToAction ("ActionName"); //This method writes directly to the page, only if the page is changed under the controller, such as Index. aspx and About. aspx above

RedirectToAction ("ActionName", "ControllerName")//This method writes directly to ActionName and ControllerName, provided that you change the controller under the page such as Index. aspx and About. aspx above


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();
  }
 }
}

Related articles: