Realization method of ASP. NET MVC 3 imitating Server. Transfer effect

  • 2021-07-06 10:47:04
  • OfStack

When we use ASP. NET MVC to implement page jump, the common use should be:

Redirect
RedirectToAction
RedirectToRoute
Or use script jumps in the foreground.
However, these jump methods are all based on Get requests, which may not be applicable in some specific scenarios. For example, in scenarios where large data volume parameters or complex object type parameters need to be passed, get mode is definitely limited.

In webform, there is a server-side jump mode: Server. Transfer. I believe everyone will remember it. This approach aborts the execution of the current page, moves the execution process to a new page, and uses the response flow created on the previous page. This method has the following characteristics:
1. The address bar URL will not change.
2. Parameters and objects generated in the background of the previous page can be directly passed to the new page.
3. Reduce the client's request to the server.

As we know, ASP. NET MVC has a core idea, that is, "convention is better than configuration". For example, after executing an action, it will go to the view directory to find the corresponding view according to the name of controller for rendering, but the convention 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 for rendering 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, the ChangeViewPathAttribute class inherits from ActionFilter, overrides the OnResultExecuting method, and adds the custom ViewEngine to the global ViewEngine collection.

Step 3: Add Attribute to action that needs 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 action at will, and jump on the server side to achieve an effect similar to Server. Transfer. Of course, the above is just a simple example. You can do it more gracefully and realize more flexible path configuration.

The above is the whole content of this paper, hoping to help everyone's study.


Related articles: