Server.Transfer alternative in ASP.NET Core

I am porting an ASP.NET application to the ASP.NET kernel and they have several calls HttpServerUtility.Transfer(string path). However, it HttpServerUtilitydoes not exist in ASP.NET Core.

Is there an alternative I can use? Or is the Response.Redirectonly option I have?

I want to support the same behavior as much as the old application, as there is a difference between Server.Transfer and Response.Redirect .

+4
source share
3 answers

I see some options for you, depending on your case:

  • . HTML. . Muqeet Khan.
  • . - . - return MyOtherAction("foo", "bar").
  • . . C. , , 90% ASP.NET Core (, , cookie, ,...).
  • : , , . .
  • . . , , . (PM ASP.NET), ASP.NET Core Kestrel/TCPIP HTML . . .

: ;). ASP.NET Core . , .

+2

. Server.Transfer Server.Redirect . Server.Transfer , , . , URL- URL-, - . , Server.Redirect, . URL-, , , URL-.

Server.Transfer Asp.Net Core, Request.Path Request.QueryString, URL-, , , url . , .

page1.html

 <html>
    <body>
        <h1>Page 1</h1>
    </body>
</html>

page2.html

 <html>
    <body>
        <h1>Page 2</h1>
    </body>
</html>

ExampleTransferController.cs

    using Microsoft.AspNetCore.Diagnostics;
    using Microsoft.AspNetCore.Http;
    using Microsoft.AspNetCore.Mvc;

    namespace App.Web.Controllers {

        public class ExampleTransferController: Controller {

            public ExampleTransferController() {

            }

            [Route("/example-transfer/page1")]
            public IActionResult Page1() {
                bool condition = true;

                if(condition) {

                    //Store the original url in the HttpContext items
                    //so that it available to the app.
                    string originalUrl = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host}{HttpContext.Request.Path}{HttpContext.Request.QueryString}";
                    HttpContext.Items.Add("OriginalUrl", originalUrl);

                    //Modify the request to indicate the url we want to transfer to
                    string newPath = "/example-transfer/page2";
                    string newQueryString = "";
                    HttpContext.Request.Path = newPath;
                    HttpContext.Request.QueryString = new QueryString(newQueryString);

                    //Now call the action method for that new url
                    //Note that instantiating the controller for the new action method
                    //isn't necessary if the action method is on the same controller as 
                    //the action method for the original request but
                    //I do it here just for illustration since often the action method you
                    //may want to call will be on a different controller.
                    var controller = new ExampleTransferController();
                    controller.ControllerContext = new ControllerContext(this.ControllerContext);
                    return controller.Page2();
                }

                return View();
            }


            [Route("/example-transfer/page2")]
            public IActionResult Page2() {

                string originalUrl = HttpContext.Items["OriginalUrl"] as string;
                bool requestWasTransfered = (originalUrl != null);

                return View();
            }

        }
    }

URL- HttpContext.Items["OriginalUrl"] , , , , URL- .

+2

, "named view" MVC. ,

[HttpPost]
public ActionResult Index(string Name)
{
 ViewBag.Message = "Some message";
 //Like Server.Transfer() in Asp.Net WebForm
 return View("MyIndex");
}

The above will return this particular view. If you have a condition that governs viewing details, you can do this.

0
source

Source: https://habr.com/ru/post/1655768/


All Articles