How to simulate Server.Transfer in ASP.NET MVC?

后端 未结 14 1401
死守一世寂寞
死守一世寂寞 2020-11-22 12:00

In ASP.NET MVC you can return a redirect ActionResult quite easily :

 return RedirectToAction(\"Index\");

 or

 return RedirectToRoute(new { controller = \"         


        
相关标签:
14条回答
  • 2020-11-22 12:24

    Just instance the other controller and execute it's action method.

    0 讨论(0)
  • 2020-11-22 12:24

    I achieved this by harnessing the Html.RenderAction helper in a View:

    @{
        string action = ViewBag.ActionName;
        string controller = ViewBag.ControllerName;
        object routeValues = ViewBag.RouteValues;
        Html.RenderAction(action, controller, routeValues);
    }
    

    And in my controller:

    public ActionResult MyAction(....)
    {
        var routeValues = HttpContext.Request.RequestContext.RouteData.Values;    
        ViewBag.ActionName = "myaction";
        ViewBag.ControllerName = "mycontroller";
        ViewBag.RouteValues = routeValues;    
        return PartialView("_AjaxRedirect");
    }
    
    0 讨论(0)
  • 2020-11-22 12:25

    Not an answer per se, but clearly the requirement would be not only for the actual navigation to "do" the equivalent functionality of Webforms Server.Transfer(), but also for all of this to be fully supported within unit testing.

    Therefore the ServerTransferResult should "look" like a RedirectToRouteResult, and be as similar as possible in terms of the class hierarchy.

    I'm thinking of doing this by looking at Reflector, and doing whatever RedirectToRouteResult class and also the various Controller base class methods do, and then "adding" the latter to the Controller via extension methods. Maybe these could be static methods within the same class, for ease/laziness of downloading?

    If I get round to doing this I'll post it up, otherwise maybe somebody else might beat me to it!

    0 讨论(0)
  • 2020-11-22 12:26

    Rather than simulate a server transfer, MVC is still capable of actually doing a Server.TransferRequest:

    public ActionResult Whatever()
    {
        string url = //...
        Request.RequestContext.HttpContext.Server.TransferRequest(url);
        return Content("success");//Doesn't actually get returned
    }
    
    0 讨论(0)
  • 2020-11-22 12:28

    Couldn't you just create an instance of the controller you would like to redirect to, invoke the action method you want, then return the result of that? Something like:

     HomeController controller = new HomeController();
     return controller.Index();
    
    0 讨论(0)
  • 2020-11-22 12:29

    Server.TransferRequest is completely unnecessary in MVC. This is an antiquated feature that was only necessary in ASP.NET because the request came directly to a page and there needed to be a way to transfer a request to another page. Modern versions of ASP.NET (including MVC) have a routing infrastructure that can be customized to route directly to the resource that is desired. There is no point of letting the request reach a controller only to transfer it to another controller when you can simply make the request go directly to the controller and action you want.

    What's more is that since you are responding to the original request, there is no need to tuck anything into TempData or other storage just for the sake of routing the request to the right place. Instead, you arrive at the controller action with the original request intact. You also can be rest assured that Google will approve of this approach as it happens entirely on the server side.

    While you can do quite a bit from both IRouteConstraint and IRouteHandler, the most powerful extension point for routing is the RouteBase subclass. This class can be extended to provide both incoming routes and outgoing URL generation, which makes it a one stop shop for everything having to do with the URL and the action that URL executes.

    So, to follow your second example, to get from / to /home/7, you simply need a route that adds the appropriate route values.

    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
            // Routes directy to `/home/7`
            routes.MapRoute(
                name: "Home7",
                url: "",
                defaults: new { controller = "Home", action = "Index", version = 7 }
            );
    
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
    

    But going back to your original example where you have a random page, it is more complex because the route parameters cannot change at runtime. So, it could be done with a RouteBase subclass as follows.

    public class RandomHomePageRoute : RouteBase
    {
        private Random random = new Random();
    
        public override RouteData GetRouteData(HttpContextBase httpContext)
        {
            RouteData result = null;
    
            // Only handle the home page route
            if (httpContext.Request.Path == "/")
            {
                result = new RouteData(this, new MvcRouteHandler());
    
                result.Values["controller"] = "Home";
                result.Values["action"] = "Index";
                result.Values["version"] = random.Next(10) + 1; // Picks a random number from 1 to 10
            }
    
            // If this isn't the home page route, this should return null
            // which instructs routing to try the next route in the route table.
            return result;
        }
    
        public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
        {
            var controller = Convert.ToString(values["controller"]);
            var action = Convert.ToString(values["action"]);
    
            if (controller.Equals("Home", StringComparison.OrdinalIgnoreCase) &&
                action.Equals("Index", StringComparison.OrdinalIgnoreCase))
            {
                // Route to the Home page URL
                return new VirtualPathData(this, "");
            }
    
            return null;
        }
    }
    

    Which can be registered in routing like:

    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
            // Routes to /home/{version} where version is randomly from 1-10
            routes.Add(new RandomHomePageRoute());
    
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
    

    Note in the above example, it might make sense to also store a cookie recording the home page version the user came in on so when they return they receive the same home page version.

    Note also that using this approach you can customize routing to take query string parameters into consideration (it completely ignores them by default) and route to an appropriate controller action accordingly.

    Additional Examples

    • https://stackoverflow.com/a/31958586
    • https://stackoverflow.com/a/36774498
    • https://stackoverflow.com/a/36168395
    0 讨论(0)
提交回复
热议问题