Asp.net REST WebAPI接口作为API调用中的参数

Asp.net REST WebAPI接口作为API调用中的参数,asp.net,rest,asp.net-web-api,interface,dependency-injection,Asp.net,Rest,Asp.net Web Api,Interface,Dependency Injection,我正在使用ASP.NET WebAPI构建REST API。一切都很好,但后来我想出了一个好主意,在我所有的方法调用中使用接口。在我更改了所有方法之后,我注意到在我的控制器方法中将参数设置为接口之后,我的API调用不起作用。我正在使用OWIN自宿主和Unity依赖注入。这是我的相关代码: 解析我的界面: IUnityContainer container = new UnityContainer(); container.RegisterType<IMyInter

我正在使用ASP.NET WebAPI构建REST API。一切都很好,但后来我想出了一个好主意,在我所有的方法调用中使用接口。在我更改了所有方法之后,我注意到在我的控制器方法中将参数设置为接口之后,我的API调用不起作用。我正在使用OWIN自宿主和Unity依赖注入。这是我的相关代码:

解析我的界面:

      IUnityContainer container = new UnityContainer();

      container.RegisterType<IMyInterface, MyInterfaceImpl>(new HierarchicalLifetimeManager());
      HttpConfiguration config = new HttpConfiguration();
      config.DependencyResolver = new UnityDependencyResolver(container);  
调用此方法时,我得到一个错误,即无法创建接口。我对此不敢苟同,但问题在于如何解决它。我看了,也看了,但没有一个建议在我的案例中起作用(总是得到无法创建接口的错误)

我想知道是否有人有一个这样的工作示例(在github或其他地方),只是为了检查我做错了什么(甚至是一个想法,我可以尝试其他什么会更好)

谢谢

也许这有助于:


您不能使用格式化程序,因为您的数据来自URI。我认为您可以使用链接中的Modelbinder方法。

因为您是从查询字符串传递数据,这里需要不同的方法。在你提到的我的博客文章中,我没有包括那个场景。由于查询字符串是通过模型绑定器处理的,因此需要创建自定义模型绑定器

在我的情况下,我选择创建一个IoCModelBinder,如下所示

public class IocModelBinder : IModelBinder
{
    public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
    {
        var targetObject = ServiceLocator.Current.GetInstance(bindingContext.ModelType);
        var valueProvider = GlobalConfiguration.Configuration.Services.GetValueProviderFactories().First(item => item is QueryStringValueProviderFactory).GetValueProvider(actionContext);

        foreach (var property in targetObject.GetType().GetProperties())
        {
            var valueAsString = valueProvider.GetValue(property.Name);
            var value = valueAsString == null ? null : valueAsString.ConvertTo(property.PropertyType);

            if (value == null)
                continue;

            property.SetValue(targetObject, value, null);
        }

        bindingContext.Model = targetObject;
        return true;
    }
}
使用中

    /// <summary>
    /// Searches by the criteria specified.
    /// </summary>
    /// <param name="searchCriteriaDto">The search criteria dto.</param>
    /// <returns></returns>
    [HttpGet]
    public HttpResponseMessage Search([ModelBinder(typeof(IocModelBinder))]IApplicationSearchCriteriaDto searchCriteriaDto)
    {

    }
//
///按指定的条件进行搜索。
/// 
///搜索条件为dto。
/// 
[HttpGet]
公共HttpResponseMessage搜索([ModelBinder(typeof(IocModelBinder))]IApplicationSearchCriteriadToSearchCriteriadTo)
{
}
希望这有帮助


Brette

因为我使用Owin作为自己的主机,所以我没有全局配置。我所拥有的只是一个HttpConfiguration,但当使用它时,我得到一个错误,即HttpConfiguration中没有接受System.Web.Http.Controllers.ServicesContainer作为参数的方法GetValueProviderFactorys
    /// <summary>
    /// Searches by the criteria specified.
    /// </summary>
    /// <param name="searchCriteriaDto">The search criteria dto.</param>
    /// <returns></returns>
    [HttpGet]
    public HttpResponseMessage Search([ModelBinder(typeof(IocModelBinder))]IApplicationSearchCriteriaDto searchCriteriaDto)
    {

    }