我的项目是基于 ASP.Net MVC Web API 4 应用程序创建的。如果它的模型具有以下实现:
public class BaseEntity
{
public object Id {get; set;}
}
public class Entity1 : BaseEntity
{
public string Name {get; set;}
}
Wep API 控制器是:
public class Entity1Controller : ApiController
{
...
[Queryable]
public IQueryable<T> Get()
{
return repository.Table.AsQueryable();
}
}
并且 Web API 配置有这个设置:
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
...
ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntitySet<BaseEntity>("BaseEntity");
modelBuilder.EntitySet<Entity1>("Entity1");
IEdmModel model = modelBuilder.GetEdmModel();
GlobalConfiguration.Configuration.SetEdmModel(model);
}
}
因此,当在客户端应用程序中生成一个请求时:
$.getJSON("api/Entity1?$filter=Id eq '" + id + "'",
function (data) {
...
});
我的应用程序遇到以下错误:
{"Message":"The query specified in the URI is not valid.","ExceptionMessage":"Type 'Entity 1' does not have a property 'Id'."}
为什么 URI 中指定的查询使用 Web API OData 无效?