V10: Build warnings in Web.Website (#12332)

* add new rule to globalconfig

* Fix warnings in Web.Website

* Fix more warnings in Web.Website

* Fix more build warnings in Web.Website

* Fix more warnings in Web.Website

* Fix tests

* Fix proper constructor call

* Fix not being able to run project

* Fix Obsolete method

Co-authored-by: Nikolaj Geisle <niko737@edu.ucl.dk>
This commit is contained in:
Nikolaj Geisle
2022-05-06 15:06:39 +02:00
committed by GitHub
parent b648177a40
commit 4f3d680f06
47 changed files with 3525 additions and 3447 deletions

View File

@@ -1,6 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Infrastructure;
@@ -10,100 +7,96 @@ using Umbraco.Cms.Core.Composing;
using Umbraco.Cms.Web.Common.Controllers;
using static Umbraco.Cms.Core.Constants.Web.Routing;
namespace Umbraco.Cms.Web.Website.Routing
namespace Umbraco.Cms.Web.Website.Routing;
/// <summary>
/// Used to find a controller/action in the current available routes
/// </summary>
public class ControllerActionSearcher : IControllerActionSearcher
{
private const string DefaultActionName = nameof(RenderController.Index);
private readonly IActionSelector _actionSelector;
private readonly ILogger<ControllerActionSearcher> _logger;
/// <summary>
/// Used to find a controller/action in the current available routes
/// Initializes a new instance of the <see cref="ControllerActionSearcher" /> class.
/// </summary>
public class ControllerActionSearcher : IControllerActionSearcher
public ControllerActionSearcher(
ILogger<ControllerActionSearcher> logger,
IActionSelector actionSelector)
{
private readonly ILogger<ControllerActionSearcher> _logger;
private readonly IActionSelector _actionSelector;
private const string DefaultActionName = nameof(RenderController.Index);
_logger = logger;
_actionSelector = actionSelector;
}
/// <summary>
/// Initializes a new instance of the <see cref="ControllerActionSearcher"/> class.
/// </summary>
public ControllerActionSearcher(
ILogger<ControllerActionSearcher> logger,
IActionSelector actionSelector)
/// <summary>
/// Determines if a custom controller can hijack the current route
/// </summary>
/// <typeparam name="T">The controller type to find</typeparam>
public ControllerActionDescriptor? Find<T>(HttpContext httpContext, string? controller, string? action) =>
Find<T>(httpContext, controller, action, null);
/// <summary>
/// Determines if a custom controller can hijack the current route
/// </summary>
/// <typeparam name="T">The controller type to find</typeparam>
public ControllerActionDescriptor? Find<T>(HttpContext httpContext, string? controller, string? action, string? area)
{
IReadOnlyList<ControllerActionDescriptor>? candidates =
FindControllerCandidates<T>(httpContext, controller, action, DefaultActionName, area);
if (candidates?.Count > 0)
{
_logger = logger;
_actionSelector = actionSelector;
return candidates[0];
}
return null;
}
/// <summary>
/// Determines if a custom controller can hijack the current route
/// </summary>
/// <typeparam name="T">The controller type to find</typeparam>
public ControllerActionDescriptor? Find<T>(HttpContext httpContext, string? controller, string? action) => Find<T>(httpContext, controller, action, null);
/// <summary>
/// Determines if a custom controller can hijack the current route
/// </summary>
/// <typeparam name="T">The controller type to find</typeparam>
public ControllerActionDescriptor? Find<T>(HttpContext httpContext, string? controller, string? action, string? area)
/// <summary>
/// Return a list of controller candidates that match the custom controller and action names
/// </summary>
private IReadOnlyList<ControllerActionDescriptor>? FindControllerCandidates<T>(
HttpContext httpContext,
string? customControllerName,
string? customActionName,
string? defaultActionName,
string? area = null)
{
// Use aspnetcore's IActionSelector to do the finding since it uses an optimized cache lookup
var routeValues = new RouteValueDictionary
{
IReadOnlyList<ControllerActionDescriptor>? candidates = FindControllerCandidates<T>(httpContext, controller, action, DefaultActionName, area);
[ControllerToken] = customControllerName,
[ActionToken] = customActionName, // first try to find the custom action
};
if (candidates?.Count > 0)
{
return candidates[0];
}
return null;
if (area != null)
{
routeValues[AreaToken] = area;
}
var routeData = new RouteData(routeValues);
var routeContext = new RouteContext(httpContext) { RouteData = routeData };
/// <summary>
/// Return a list of controller candidates that match the custom controller and action names
/// </summary>
private IReadOnlyList<ControllerActionDescriptor>? FindControllerCandidates<T>(
HttpContext httpContext,
string? customControllerName,
string? customActionName,
string? defaultActionName,
string? area = null)
// try finding candidates for the custom action
var candidates = _actionSelector.SelectCandidates(routeContext)?
.Cast<ControllerActionDescriptor>()
.Where(x => TypeHelper.IsTypeAssignableFrom<T>(x.ControllerTypeInfo))
.ToList();
if (candidates?.Count > 0)
{
// Use aspnetcore's IActionSelector to do the finding since it uses an optimized cache lookup
var routeValues = new RouteValueDictionary
{
[ControllerToken] = customControllerName,
[ActionToken] = customActionName, // first try to find the custom action
};
if (area != null)
{
routeValues[AreaToken] = area;
}
var routeData = new RouteData(routeValues);
var routeContext = new RouteContext(httpContext)
{
RouteData = routeData
};
// try finding candidates for the custom action
var candidates = _actionSelector.SelectCandidates(routeContext)?
.Cast<ControllerActionDescriptor>()
.Where(x => TypeHelper.IsTypeAssignableFrom<T>(x.ControllerTypeInfo))
.ToList();
if (candidates?.Count > 0)
{
// return them if found
return candidates;
}
// now find for the default action since we couldn't find the custom one
routeValues[ActionToken] = defaultActionName;
candidates = _actionSelector.SelectCandidates(routeContext)?
.Cast<ControllerActionDescriptor>()
.Where(x => TypeHelper.IsTypeAssignableFrom<T>(x.ControllerTypeInfo))
.ToList();
// return them if found
return candidates;
}
// now find for the default action since we couldn't find the custom one
routeValues[ActionToken] = defaultActionName;
candidates = _actionSelector.SelectCandidates(routeContext)?
.Cast<ControllerActionDescriptor>()
.Where(x => TypeHelper.IsTypeAssignableFrom<T>(x.ControllerTypeInfo))
.ToList();
return candidates;
}
}