Q:

C# - middleware that adds field value to controller argument if argument inherits from interface

I could use some advise or pointers. My middleware knowledge is failing me today.
Suppose I have a controller endpoint that looks like this
public int Create([FromBody] InputDto InputDto)

And that InputDto looks like this
public class InputDto : IHasSpecialThingy
{
    public SpecialThingy SpecialThingy { get; set; }
    // Plus some other cool fields
}

What I am trying to achieve is some intermediate middleware that checks when an object inherits from "IHasSpecialThingy", add SpecialThingy onto it.. 
I have tried creating my own IModelBinder, with little success.
Middleware is unfortunately not my strong suit. Any advice would be greatly appreciated.
Thank you in advance for any help.
EDIT
I started with a IActionFilter with a custom implementation. It should be good. Need to still figure out some Dependency injection for it. I will post an answer when I have cleaned it up a lot. Will still keep it open for quite some while as someone might have a better solution me.

A:

I have found a solution to the problem. Please see the code below.
public class SpecialThingyFilter : IActionFilter
{
    public bool AllowMultiple { get; }

    public async Task<HttpResponseMessage> ExecuteActionFilterAsync(HttpActionContext actionContext, CancellationToken cancellationToken, Func<Task<HttpResponseMessage>> continuation)
    {
        var commandsWithSpecialThingy = actionContext.ActionArguments
            .Where(x => x.Value != null && x.Value.GetType().GetInterfaces().Contains(typeof(IHasSpecialThingy)))
            .Select(x => x.Value).ToList();

        if (!commandsWithSpecialThingy.Any())
        {
            return await continuation.Invoke();
        }

        foreach (var dto in commandsWithSpecialThingy)
        {
            //Do your magic stuffs here
            ((IHasSpecialThingy)dto).specialThingy = // Something special
        }

        return await continuation.Invoke();
    }
}

You can also use DI in the action filter if your DI container is setup correctly.
var specialtyService = actionContext.ControllerContext
      .Configuration.DependencyResolver
      .GetService(typeof(ISpecialtyService)) as ISpecialtyService;

Please let me know if you have any questions or if you come across his and need some help with a similar issue.

