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:
- Url Action Routing, with custom routes
- 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