共用方式為


TransferToAction for ASP.NET MVC 5

In addition to the RedirectToAction method inside a Controller, I want to be able to use a Server.Transfer inside my actions.
This can be useful, if you want to redirect without changing the URL(routing).
I also want to ensure that an action is only accessible by a transfer request - similar to ChildActionOnly-attribute.

After searching for an existing solution without success (not a complete solution) I came up with the following code.

The usage is very simple:

The transfer implementation is inside a custom ActionResult and can be easily called trough an extension.

To make the TransferActionOnly-attribute possible, a query string marker is used.

You can do anything you want with this code, but don’t hold me liable :)

Comments

  • Anonymous
    August 15, 2017
    While trying to implement this solution I noticed that the route data was not being correctly forwarded to the TransferRequest method, the is fix to remove the double wrapping of the route data in the TransferToRouteResult constructor like sopublic TransferToRouteResult(string actionName, string controllerName, RouteValueDictionary routeValues) { ActionName = actionName; ControllerName = controllerName; RouteValues = routeValues ?? new RouteValueDictionary(); }