How to achieve the same effect of ASP.NET MVC Server.Transfer in ASP.NET Core MVC?

I’m new to this kind of migration. Kindly help me!

TransferResult: is this required in ASP.NET Core 8.0 MVC while migrating from ASP.NET MVC? (based on Markus Olsson answer)

If yes, how to alter the code to suit the migration from .NET 4.8 to .NET 8.0?

I’m getting these errors (some are obvious that they are no longer in use – I need help to figure it out):

The name ‘HttpRuntime’ does not exist in the current context

The type or namespace name ‘MvcHttpHandler’ could not be found (are you missing a using directive or an assembly reference?)

‘HttpContext’ does not contain a definition for ‘Server’ and no accessible extension method ‘Server’ accepting a first argument of type ‘HttpContext’ could be found (are you missing a using directive or an assembly reference?)

‘HttpContext’ does not contain a definition for ‘RewritePath’ and no accessible extension method ‘RewritePath’ accepting a first argument of type ‘HttpContext

The type or namespace name ‘IHttpHandler’ could not be found (are you missing a using directive or an assembly reference?)

Need help here

2

The HttpRuntime class, is now scattered among several other services and classes thoughout the .net 8, the architecture has changed, so the HttpRuntime.UsingIntegratedPipeline property for example in the classic ASP.NET framework was used to check if the application was running under IIS’s integrated pipeline mode. This property is no longer applicable in ASP.NET Core because ASP.NET Core uses a completely different request processing pipeline and can run cross-platform, decoupled from IIS.

In ASP.NET Core, there is no equivalent concept of “integrated pipeline” because the hosting model is different.

That also goes for the Server.Transfer concept, since it’s not a HTTP 302 redirect its only a “reinterpretation” of the request by the server itself, and the pipeline is not handled by outside software, you can achieve the same behaviors with:

  1. Url Action Routing, with custom routes
  2. Middlewares, with custom middlewares

Trying to not use any of those solutions would mean to break the MVC lifecycle, I don’t really know if there is builtin a solution to inform the middleware that it needs to call another action instead of the one that it’s actually calling.

But… I could come up with a “migration”, “solution” or a “gambiarra” (a technical circunvention) to use a more Brazilian term, to the old code in Markus Olsson answer, don’t actually know if it’s gonna work in every scenario, here it is:

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Routing;

public class TransferActionResult : ActionResult
{
    private readonly string? _routeName;
    private readonly RouteValueDictionary _routeValues;

    public TransferActionResult(RouteValueDictionary routeValues) : this (null, routeValues) { }
    
    public TransferActionResult(string? routeName, RouteValueDictionary routeValues)
    {
        _routeName = routeName;
        _routeValues = routeValues;
    }
    
    public override Task ExecuteResultAsync(ActionContext context)
    {
        var urlHelperFactory = context.HttpContext.RequestServices.GetRequiredService<IUrlHelperFactory>();
        var urlHelper = urlHelperFactory.GetUrlHelper(context);
        var url = urlHelper.RouteUrl(_routeName, _routeValues);
        
        context.HttpContext.Request.Path = url;
        
        string? controllerName = _routeValues["controller"]?.ToString();
        string? actionName = _routeValues["action"]?.ToString();
        
        if(string.IsNullOrWhiteSpace(controllerName))
            throw new Exception("Could not find controllerName in routingValues");
        
        if(string.IsNullOrWhiteSpace(actionName))
            throw new Exception("Could not find actionName in routingValues");
        
        var actionDescriptor = context.HttpContext.RequestServices.GetRequiredService<IActionDescriptorCollectionProvider>().ActionDescriptors.Items
            .OfType<ControllerActionDescriptor>()
            .FirstOrDefault(ad => ad.ControllerName == controllerName && ad.ActionName == actionName);

        if (actionDescriptor == null)
            throw new Exception("Could not find action descriptor");

        var actionContext = new ActionContext(context.HttpContext, new RouteData(_routeValues), actionDescriptor);
        
        var actionInvoker = context.HttpContext.RequestServices.GetRequiredService<IActionInvokerFactory>()
            .CreateInvoker(actionContext);

        if (actionInvoker == null)
            throw new Exception("Could not find action action invoker");
        
        return actionInvoker.InvokeAsync();
    }
}

As you can see it tries to find the controller and action and reexecutes its action using an ActionInvoker class

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật