UmbracoRouteValueTransformer fixes
This commit is contained in:
@@ -38,7 +38,7 @@ namespace Umbraco.Extensions
|
||||
builder.Services.AddDataProtection();
|
||||
builder.Services.AddAntiforgery();
|
||||
|
||||
builder.Services.AddScoped<UmbracoRouteValueTransformer>();
|
||||
builder.Services.AddSingleton<UmbracoRouteValueTransformer>();
|
||||
builder.Services.AddSingleton<IControllerActionSearcher, ControllerActionSearcher>();
|
||||
builder.Services.AddSingleton<IUmbracoRouteValuesFactory, UmbracoRouteValuesFactory>();
|
||||
builder.Services.AddSingleton<IRoutableDocumentFilter, RoutableDocumentFilter>();
|
||||
|
||||
@@ -92,31 +92,41 @@ namespace Umbraco.Cms.Web.Website.Routing
|
||||
// If we aren't running, then we have nothing to route
|
||||
if (_runtime.Level != RuntimeLevel.Run)
|
||||
{
|
||||
return values;
|
||||
return null;
|
||||
}
|
||||
// will be null for any client side requests like JS, etc...
|
||||
if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext))
|
||||
if (!_umbracoContextAccessor.TryGetUmbracoContext(out IUmbracoContext umbracoContext))
|
||||
{
|
||||
return values;
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!_routableDocumentFilter.IsDocumentRequest(httpContext.Request.Path))
|
||||
{
|
||||
return values;
|
||||
return null;
|
||||
}
|
||||
|
||||
// Don't execute if there are already UmbracoRouteValues assigned.
|
||||
// This can occur if someone else is dynamically routing and in which case we don't want to overwrite
|
||||
// the routing work being done there.
|
||||
UmbracoRouteValues umbracoRouteValues = httpContext.Features.Get<UmbracoRouteValues>();
|
||||
if (umbracoRouteValues != null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// Check if there is no existing content and return the no content controller
|
||||
if (!umbracoContext.Content.HasContent())
|
||||
{
|
||||
values[ControllerToken] = ControllerExtensions.GetControllerName<RenderNoContentController>();
|
||||
values[ActionToken] = nameof(RenderNoContentController.Index);
|
||||
|
||||
return values;
|
||||
return new RouteValueDictionary
|
||||
{
|
||||
[ControllerToken] = ControllerExtensions.GetControllerName<RenderNoContentController>(),
|
||||
[ActionToken] = nameof(RenderNoContentController.Index)
|
||||
};
|
||||
}
|
||||
|
||||
IPublishedRequest publishedRequest = await RouteRequestAsync(httpContext, umbracoContext);
|
||||
|
||||
UmbracoRouteValues umbracoRouteValues = await _routeValuesFactory.CreateAsync(httpContext, publishedRequest);
|
||||
umbracoRouteValues = await _routeValuesFactory.CreateAsync(httpContext, publishedRequest);
|
||||
|
||||
// Store the route values as a httpcontext feature
|
||||
httpContext.Features.Set(umbracoRouteValues);
|
||||
@@ -125,16 +135,27 @@ namespace Umbraco.Cms.Web.Website.Routing
|
||||
PostedDataProxyInfo postedInfo = GetFormInfo(httpContext, values);
|
||||
if (postedInfo != null)
|
||||
{
|
||||
return HandlePostedValues(postedInfo, httpContext, values);
|
||||
return HandlePostedValues(postedInfo, httpContext);
|
||||
}
|
||||
|
||||
values[ControllerToken] = umbracoRouteValues.ControllerName;
|
||||
// See https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.routing.dynamicroutevaluetransformer.transformasync?view=aspnetcore-5.0#Microsoft_AspNetCore_Mvc_Routing_DynamicRouteValueTransformer_TransformAsync_Microsoft_AspNetCore_Http_HttpContext_Microsoft_AspNetCore_Routing_RouteValueDictionary_
|
||||
// We should apparenlty not be modified these values.
|
||||
// So we create new ones.
|
||||
var newValues = new RouteValueDictionary
|
||||
{
|
||||
[ControllerToken] = umbracoRouteValues.ControllerName
|
||||
};
|
||||
if (string.IsNullOrWhiteSpace(umbracoRouteValues.ActionName) == false)
|
||||
{
|
||||
values[ActionToken] = umbracoRouteValues.ActionName;
|
||||
newValues[ActionToken] = umbracoRouteValues.ActionName;
|
||||
}
|
||||
|
||||
return values;
|
||||
// NOTE: If we are never returning null it means that it is not possible for another
|
||||
// DynamicRouteValueTransformer to execute to set the route values. This one will
|
||||
// always win even if it is a 404 because we manage all 404s via Umbraco and 404
|
||||
// handlers.
|
||||
|
||||
return newValues;
|
||||
}
|
||||
|
||||
private async Task<IPublishedRequest> RouteRequestAsync(HttpContext httpContext, IUmbracoContext umbracoContext)
|
||||
@@ -196,11 +217,14 @@ namespace Umbraco.Cms.Web.Website.Routing
|
||||
};
|
||||
}
|
||||
|
||||
private RouteValueDictionary HandlePostedValues(PostedDataProxyInfo postedInfo, HttpContext httpContext, RouteValueDictionary values)
|
||||
private RouteValueDictionary HandlePostedValues(PostedDataProxyInfo postedInfo, HttpContext httpContext)
|
||||
{
|
||||
// set the standard route values/tokens
|
||||
values[ControllerToken] = postedInfo.ControllerName;
|
||||
values[ActionToken] = postedInfo.ActionName;
|
||||
var values = new RouteValueDictionary
|
||||
{
|
||||
[ControllerToken] = postedInfo.ControllerName,
|
||||
[ActionToken] = postedInfo.ActionName
|
||||
};
|
||||
|
||||
ControllerActionDescriptor surfaceControllerDescriptor = _controllerActionSearcher.Find<SurfaceController>(httpContext, postedInfo.ControllerName, postedInfo.ActionName);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user