As I understand you want the controller-action pair declared in the current project (the entry project) to override the controller-action pair declared in the class library.
This is a bit tricky and requires your certain design to handle this kind of ambiguity between at least 2 controller-action pairs (for the conventional routing). When it comes to design, it's hard to say everything in details so I assume that 2 controller-action pairs are considered as duplicates and would cause an ambiguity when selecting an action at runtime if they:
- Have the same controller name (case-insensitive)
- Have the same action name (case-insensitive)
- Support one same http method (get, post, ...)
The action method's parameters don't matter (so e.g: Get
will match Get(int id)
, ...).
Actually that's the logic the default action matching would use when finding the best matched action. I'm not so sure about the other criteria if any but the point here is to show you how to resolve this ambiguity by picking the best matched action (the one defined in the current executing assembly not in the class libraries).
So the idea here is to create your custom IApplicationModelConvention
. The custom convention can help you apply some custom conventions affecting the loaded controllers, actions, routing ... With that convention, we have access to all ControllerModels
which each one exposes its own set of valid ActionModels
. So by removing the ActionModel
that you don't want, you can resolve the ambiguity issue (because there is just one remaining ActionModel
).
Here is the detail:
public class DuplicateActionsRemovalApplicationModelConvention : IApplicationModelConvention
{
public void Apply(ApplicationModel application)
{
var currentExecutingAsm = Assembly.GetExecutingAssembly();
var overriddenActions = application.Controllers.GroupBy(e => e.ControllerName)
//select only duplicate controllers
.Where(g => g.Count() > 1)
.SelectMany(g => g.SelectMany(e => e.Actions)
.GroupBy(o => o, ActionModelMatchingEqualityComparer.Instance)
//select only duplicate actions
.Where(k => k.Count() > 1)
.SelectMany(e => e.OrderByDescending(x => x.Controller.ControllerType.Assembly == currentExecutingAsm)
//select all except the action defined in the current executing assembly
.Skip(1)))
.Select(e => (controller: e.Controller, action: e));
//for each overridden action, just remove it from the owner controller
//this effectively means that the removed action will be overridden
//by the only remaining one (defined in the current executing assembly)
foreach(var overriddenAction in overriddenActions)
{
overriddenAction.controller.Actions.Remove(overriddenAction.action);
}
}
}
Here is the custom IEqualityComparer
for ActionModel
used in the code above. It helps match the 2 actions by the logic we described in the beginning.
public class ActionModelMatchingEqualityComparer : IEqualityComparer<ActionModel>
{
static readonly Lazy<ActionModelMatchingEqualityComparer> _instanceLazy =
new Lazy<ActionModelMatchingEqualityComparer>(() => new ActionModelMatchingEqualityComparer());
public static ActionModelMatchingEqualityComparer Instance => _instanceLazy.Value;
public bool Equals(ActionModel x, ActionModel y)
{
return string.Equals(x.ActionName, y.ActionName, StringComparison.OrdinalIgnoreCase) &&
x.Attributes.OfType<IActionHttpMethodProvider>().SelectMany(e => e.HttpMethods).DefaultIfEmpty("GET")
.Intersect(y.Attributes.OfType<IActionHttpMethodProvider>().SelectMany(e => e.HttpMethods).DefaultIfEmpty("GET"),
StringComparer.OrdinalIgnoreCase).Any();
}
public int GetHashCode(ActionModel obj)
{
return (obj.ActionName?.GetHashCode() ?? 0) ^
obj.Attributes.OfType<IActionHttpMethodProvider>()
.SelectMany(e => e.HttpMethods.Select(o => o.ToLower())).Distinct()
.OrderBy(e => e).Aggregate(0, (c, e) => (c * 13) ^ e.GetHashCode());
}
}
In the code above, we use the type IActionHttpMethodProvider
which is implemented by all HttpMethodAttribute
(e.g: HttpMethodAttribute
itself, HttpPostAttribute
, ...) to check if the 2 actions support at least one same http method.
Finally you can register your custom IApplicationModelConvention
globally, like this (code for .net core 2.2):
//code in ConfigureServices method in Startup.cs
services.AddMvc(o => {
//...
o.Conventions.Add(new DuplicateActionsRemovalApplicationModelConvention());
//...
});
Now when you run your project, all the duplicate actions in the referenced class libraries will be removed and overridden by the one (considered as matched with the overridden actions) defined in the current executing assembly. Note that this current executing assembly is also the one containing the Startup.cs
(the main/entry project) because that's where you add your custom convention DuplicateActionsRemovalApplicationModelConvention