1

这篇文章中,迈克·沃森说:

"除了 ParameterBindingAttribute,还有另一个钩子用于添加自定义 HttpParameterBinding。在 HttpConfiguration 对象上"

但是我的 Web API 应用程序中有三个 HttpConfiguration 对象,即:

public static void Register(HttpConfiguration config, IWindsorContainer container) <-- in WebApiConfig.cs
private static void MapRoutes(HttpConfiguration config) <-- ""
public static void ConfigureWindsor(HttpConfiguration configuration) <-- in Global.asax.cs

我应该使用哪些(配置、配置或配置)(如果有)?

更新

我试过这个,在“if”行上有一个断点:

public static void ConfigureWindsor(HttpConfiguration configuration)
{
    _container = new WindsorContainer();
    _container.Install(FromAssembly.This());
    _container.Kernel.Resolver.AddSubResolver(new CollectionResolver(_container.Kernel, true));
    var dependencyResolver = new WindsorDependencyResolver(_container);
    configuration.DependencyResolver = dependencyResolver;

    if (configuration.Properties.Values.Count > 0) // <-- I put a Casey Jones here
    {
        object o = configuration.Properties.Values.ElementAt(configuration.Properties.Values.Count - 1);
        string s = o.ToString();
    }
}

...但是我只在服务器启动时到达该位置一次,但不是在客户端向它发送请求时...当服务器传递可以传入 URL 的请求时,一定有一些事件会被触发检查...不是吗?

4

1 回答 1

2

通常你只有一个 HttpConfiguration 实例,它是你从 GlobalConfiguration.Configuration 获得的。

这么说,这就是我插入自定义活页夹的方式

在 global.asax

var binderMappings = new Dictionary<Type, Type>
    {
        {typeof(YourModelType), typeof(YourModelTypeBinder)},
        //....
    };

config.Services.Add(
    typeof(ModelBinderProvider),
    new WindsorModelBinderProvider(container, binderMappings));

WindsorModelBinderProvider

public class WindsorModelBinderProvider : ModelBinderProvider
{
    private readonly IWindsorContainer _container;
    private readonly IDictionary<Type, Type> _binderMappings;

    public WindsorModelBinderProvider(IWindsorContainer container, IDictionary<Type, Type> binderMappings)
    {
        _container = container;
        _binderMappings = binderMappings;
    }

    public override IModelBinder GetBinder(HttpConfiguration configuration, Type modelType)
    {
        IModelBinder binder = null;
        if (_binderMappings.ContainsKey(modelType))
        {
            binder = _container.Resolve(_binderMappings[modelType]) as IModelBinder;

            if (binder == null)
            {
                throw new ComponentNotFoundException(modelType);
            }
        }

        return binder;
    }
}   

YourModelTypeBinder

public class YourModelTypeBinder : IModelBinder
{
    public YourModelTypeBinder(IYourServiceToLoadYourModelType service)
    {
        //...
    }

    public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
    {
        bindingContext.Model = YourCustomCodeToLoadYourModelTypeUsingTheConstructorDependecies(actionContext.Request);
        return true;
    }

    private YourModelType YourCustomCodeToLoadYourModelTypeUsingTheConstructorDependecies(HttpRequestMessage requestMessage)
    {
        ...
    }
}

YourModelTypeBinder 将由容器解析(参见 WindsorModelBinderProvider),因此您需要先注册它。

在所有这些管道之后,您的控制器可能有一个参数等,如下所示

[ModelBinder]YourModelType user
于 2014-02-03T11:19:51.040 回答