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】
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!

How to build applications using .NET? Building applications using .NET can be achieved through the following steps: 1) Understand the basics of .NET, including C# language and cross-platform development support; 2) Learn core concepts such as components and working principles of the .NET ecosystem; 3) Master basic and advanced usage, from simple console applications to complex WebAPIs and database operations; 4) Be familiar with common errors and debugging techniques, such as configuration and database connection issues; 5) Application performance optimization and best practices, such as asynchronous programming and caching.

C# is widely used in enterprise-level applications, game development, mobile applications and web development. 1) In enterprise-level applications, C# is often used for ASP.NETCore to develop WebAPI. 2) In game development, C# is combined with the Unity engine to realize role control and other functions. 3) C# supports polymorphism and asynchronous programming to improve code flexibility and application performance.

C# and .NET are suitable for web, desktop and mobile development. 1) In web development, ASP.NETCore supports cross-platform development. 2) Desktop development uses WPF and WinForms, which are suitable for different needs. 3) Mobile development realizes cross-platform applications through Xamarin.

The C#.NET ecosystem provides rich frameworks and libraries to help developers build applications efficiently. 1.ASP.NETCore is used to build high-performance web applications, 2.EntityFrameworkCore is used for database operations. By understanding the use and best practices of these tools, developers can improve the quality and performance of their applications.

How to deploy a C# .NET app to Azure or AWS? The answer is to use AzureAppService and AWSElasticBeanstalk. 1. On Azure, automate deployment using AzureAppService and AzurePipelines. 2. On AWS, use Amazon ElasticBeanstalk and AWSLambda to implement deployment and serverless compute.

The combination of C# and .NET provides developers with a powerful programming environment. 1) C# supports polymorphism and asynchronous programming, 2) .NET provides cross-platform capabilities and concurrent processing mechanisms, which makes them widely used in desktop, web and mobile application development.

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

C# is a modern, object-oriented programming language developed by Microsoft, and .NET is a development framework provided by Microsoft. C# combines the performance of C and the simplicity of Java, and is suitable for building various applications. The .NET framework supports multiple languages, provides garbage collection mechanisms, and simplifies memory management.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Atom editor mac version download
The most popular open source editor

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
