Home  >  Article  >  Backend Development  >  Explanation of ASP code for MVC page redirection

Explanation of ASP code for MVC page redirection

Y2J
Y2JOriginal
2017-05-05 12:00:061098browse

This article mainly introduces the relevant information of ASP.NET MVC page redirection in detail, which has certain reference value. Interested friends can refer to the

page in asp.net Redirect: Server.Execute("m2.aspx"); After the server saves the data before the redirection of this page, it redirects the page to m2.aspx for execution, and then returns to this page to continue execution. The results of the three are then combined and returned to the browser. Server.
The above are all server-side page redirections, so the browser does not show page change records (the displayed address will not change). Therefore, if the user refreshes this page, some other unexpected situations may occur. Such page redirection can complete some other functions, such as accessing the server control in the previous page.

1. Response.Redirect:

When the browser requests the aspx page and encounters the Redirect(url) method, it is equivalent to telling the browser that you A page needs to be accessed first, so the browser then sends a request to the server for this page. Relocation is performed through the browser, resulting in an additional round trip 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 excess bandwidth.

Summary, when the network status is good, the Redirect(url) method is the most efficient!! The Server.Transfer method and the Server.Execute method are the most flexible!! Server. The Execute method takes up the most resources.

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

1 response.redirect This method of jumping to the page does not jump very quickly. , because it takes 2 round trips (2 postbacks), but it can jump to any page without site page restrictions (that is, it can jump from Yahoo to Sina), and it cannot skip login protection. But slow speed is its biggest flaw! Redirect jump mechanism: First, an http request is sent to the client to notify that it needs 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 saved in the internal space will be lost after the jump, so session needs to be used.
2 server.transfer is fast and only requires one postback, but. . . . It must be under the same site because it is a method of the server. In addition, he can bypass login protection. You can try writing a small program: design a jump from page one to page two, but to enter page two 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 of this method occurs on the server side, so the URL address of the browser still retains the address of the original page!
3 sever.execute This method is mainly used in page design, and it must jump to pages under the same site. This method is used when the output results of one page need to be inserted into another aspx page. Most of them are in table, where a certain page exists in another page in a nested manner.

3. How to choose page redirection method

There are four ways to jump to pages in asp.netNavigation. 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 best.
·If you want to use a program to control the conversion target, but the timing of conversion is determined by the user, use the HyperLink control of the Web server to dynamically set its NavigateUrl property.
·If you want to connect the 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 it is necessary to retain the querystring as part of the URL and pass it to the server, because the other two methods cannot achieve two postbacks, the data must be brought back to the server first. Use 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 communication, thereby obtaining better performance and Browse the effects.
·If you want to capture the output results of an ASPX page and then insert the results into a specific location on another ASPX page, use Server.Execute.
·If you want to ensure that the HTML output is legal, please use Response.Redirect, do not use the Server.Transfer or Server.Execute method.
By the way, how to use the redirect method to use Chinese characters in the query string, because often garbled characters appear because the URL does not support Chinese characters. This time you need to convert:
string message =server.urlencode("Welcome");
Convert first, then use the query string
response .redirect("webform2.aspx?msg="+message);
About Server.Execute
This page navigation method is similar to a function for ASPX pages Call, the called page can access the form data and query string collection of the calling page, so the EnableViewStateMac property of the Page command of the called page must be set to False.
By default, the output of the called page is appended to the current response stream. However, the Server.Execute method has an overloaded method that allows the output of the called page to be obtained through a TextWriter object (or its sub-object, such as a StringWriter object) instead of Append directly to the output stream, so that the position of the output result of the called page can be easily adjusted in the original page.

MVC page redirection is very 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"] = "欢迎使用 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"] = "欢迎使用 ASP.NET MVC!";
   return Redirect("User/News");
  }

  public ActionResult About()
  {
   return View();
  }
 }
}

3.Return RedirectToAction();Method

This method has two overloads

RedirectToAction("ActionName");//This method writes directly to the page, the premise must be to change the page under the controller such as the previous Index .aspx, and About.aspx

RedirectToAction("ActionName","ControllerName")//This method directly writes ActionName and ControllerName, the premise must be to change the page under the controller, such as the previous 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"] = "欢迎使用 ASP.NET MVC!";
   return RedirectToAction("News","User");
  }

  public ActionResult About()
  {
   return View();
  }
 }
}

【Related recommendations】

1. ASP Free Video Tutorial

2. ASP Tutorial

3. Li Yanhui ASP basic video tutorial

The above is the detailed content of Explanation of ASP code for MVC page redirection. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn