ASP. net mvc 3 implements the effect of Server. Transfer, mvcserver. transfer

Source: Internet
Author: User

ASP. net mvc 3 implements the effect of Server. Transfer, mvcserver. transfer

When we use ASP. net mvc to implement page Jump, we usually use the following:

Redirect
RedirectToAction
RedirectToRoute
Or use a script to jump to the front-end.
However, these jump methods are based on Get requests and may not be applicable in some specific scenarios. For example, if you want to transmit parameters of a large data volume or complex object type, the get method must be limited.

In webform, there is a Server jump Method: Server. Transfer. I believe everyone will remember it. This method suspends the execution of the current page, transfers the execution process to a new page, and uses the response stream created on the previous page.This method has the following features:
1. the URL in the address bar will not change.
2. Parameters and objects generated in the background of the previous page can be directly transmitted to the new page.
3. Reduce client requests to the server.

We know that ASP. net mvc has a core idea:"Conventions are better than configurations", For example, after an action is executed, it will go to the view directory to find the corresponding view based on the controller name for rendering, but the agreed approach does not mean that it cannot be changed.

For ASP. net mvc, You can dynamically change the view path rendered by the current Action to achieve similar results.

Rendering views of unconventional paths

Step 1: First implement a custom ViewEngine:

public class ChangeViewEngine : System.Web.Mvc.RazorViewEngine  {    public ChangeViewEngine(string controllerPathName,string viewName)    {      this.ViewLocationFormats = new[] {"~/Views/" + controllerPathName + "/" + viewName + ".cshtml" };          }  }

Step 2: Implement an ActionAttribute

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]  public class ChangeViewPathAttribute : ActionFilterAttribute  {    private string _controllerPath;    private string _viewName;    public ChangeViewPathAttribute(string controllerPath,string viewName)    {      this._controllerPath = controllerPath;      this._viewName = viewName;    }    public override void OnResultExecuting(ResultExecutingContext filterContext)    {      //base.OnResultExecuting(filterContext);      //ViewEngines.Engines.Clear();            ViewEngines.Engines.Add(new ChangeViewEngine(_controllerPath,_viewName));    }  }

In this Code segment, the changeviewpathattriing class inherits from ActionFilter and overwrites the OnResultExecuting method to add the custom ViewEngine to the global ViewEngine set.

Step 3: Add Attribute to the action to render different paths

    [HttpPost]    [Filter.ChangeViewPath("Invoice","Create")]    public ActionResult PreInvoice(string strIds,bool flag)

After completing the above steps, we can specify the view to be rendered by the action at will and jump to the Server side to achieve the effect similar to Server. Transfer. Of course, the above is just a simple example. You can do more elegantly and implement more flexible path configuration.

The above is all the content of this article, hoping to help you learn.

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.