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

后端 未结 14 1567
死守一世寂寞
死守一世寂寞 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:39

    I found out recently that ASP.NET MVC doesn't support Server.Transfer() so I've created a stub method (inspired by Default.aspx.cs).

        private void Transfer(string url)
        {
            // Create URI builder
            var uriBuilder = new UriBuilder(Request.Url.Scheme, Request.Url.Host, Request.Url.Port, Request.ApplicationPath);
            // Add destination URI
            uriBuilder.Path += url;
            // Because UriBuilder escapes URI decode before passing as an argument
            string path = Server.UrlDecode(uriBuilder.Uri.PathAndQuery);
            // Rewrite path
            HttpContext.Current.RewritePath(path, false);
            IHttpHandler httpHandler = new MvcHttpHandler();
            // Process request
            httpHandler.ProcessRequest(HttpContext.Current);
        }
    

提交回复
热议问题