【问题标题】:MVC WebApi not working with Generic modelsMVC WebApi 不适用于通用模型
【发布时间】:2012-08-21 11:43:27
【问题描述】:

我将 mij WCF WebApi 项目转换为 MVC WebApi 项目。我有一个具有这样属性的模型:

public class ObjectWaarde<T> 
{

    public ObjectWaarde(T objectType)
    {
        ObjectType = objectType;
    }

    /// <summary>
    /// Gets the term to search for.
    /// </summary>
    public T ObjectType { get; set; }

    public object Waardes
    {
        get
        {

            return ObjectType;
        }
        set { ObjectType = (T) value; }
    }
}

当我将此模型发布到我的 ApiController 时,会出现此错误 (HTTP 500):

对象“System.RuntimeType”上的属性访问器“DeclaringMethod”引发以下异常:“只能在 Type.IsGenericParameter 为 true 的类型上调用方法。”

【问题讨论】:

    标签: model asp.net-mvc-4 asp.net-web-api model-binding customvalidator


    【解决方案1】:

    问题是由默认的bodymodelvalidator引起的。

    这是一个已知问题: http://aspnetwebstack.codeplex.com/workitem/225

    我找到的唯一解决方案是禁用模型验证器。

    GlobalConfiguration.Configuration.Services.Clear(typeof(IBodyModelValidator));

    【讨论】:

      猜你喜欢
      • 2020-04-18
      • 2015-04-08
      • 1970-01-01
      • 2015-10-17
      • 1970-01-01
      • 1970-01-01
      • 2017-11-06
      • 1970-01-01
      • 2013-06-26
      相关资源
      最近更新 更多