asp.net MVC 3 The implementation method of imitation Server.Transfer effect _ practical skills

Source: Internet
Author: User

When we are using the ASP.net MVC implementation page jump, the common should be:

Redirect
Redirecttoaction
Redirecttoroute
or use a script jump in the foreground.
However, these types of jumps are based on get requests and may not be applicable in certain scenarios. For example, a scenario that requires passing large data parameters, or complex object type parameters, is definitely limited to the GET method.

Inside the WebForm, there is a server-side jump way: Server.Transfer, I believe we must all remember. This approach is to abort the current page execution and move the execution process to a new page and use the response stream created by the previous page. This approach has the following characteristics:
1, the Address bar URL does not change.
2, the previous page background generated parameters and objects can be passed directly to the new page.
3, reduce client requests to the server.

We know that asp.net mvc has a core idea that "conventions are better than configurations ", for example, after an action is executed, it is rendered by looking at the corresponding view according to the controller name in the view directory, but The agreed practice does not mean that it cannot be changed.

For ASP.net mvc, a similar effect can be achieved by dynamically changing the view path rendered by the current action.

View that renders a non-normal path

The first step is to implement a custom viewengine:

public class ChangeViewEngine:System.Web.Mvc.RazorViewEngine
  {public
    changeviewengine (string Controllerpathname,string viewName)
    {this
      . Viewlocationformats = new[] {"~/views/" + controllerpathname + "/" + ViewName + ". cshtml"};
      
    }
  }

The second step is to realize a 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, the Changeviewpathattribute class inherits from the Actionfilter and overrides the Onresultexecuting method, adding the custom viewengine to the global Viewengine collection.

The third step, where you need to render different paths for the action plus attribute

    [HttpPost]
    [Filter.changeviewpath ("Invoice", "Create")]
    Public ActionResult Preinvoice (string Strids,bool flag)

After the above steps, we are free to specify the action to render the view, on the server to jump to achieve similar server.transfer effect. Of course, the above is just a simple example, you can do a bit more elegant, to achieve a more flexible path configuration.

The above is the entire content of this article, I hope to help you learn.

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.