Mohsen Alikhani
Mohsen Alikhani

Reputation: 1666

The query specified in the URI is not valid using Web API OData

My project is created based on ASP.Net MVC Web API 4 application. If it's model have the following implementations :

public class BaseEntity
{
    public  object Id {get; set;}
}

public class Entity1 : BaseEntity
{
    public  string Name {get; set;}
}

And Wep API controller is :

public class Entity1Controller : ApiController
{
    ...

    [Queryable]
    public IQueryable<T> Get()
    {
        return repository.Table.AsQueryable();
    }
}

And Web API configuration have this setting :

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);
    }
}

so, when one request is generated in the client side application :

$.getJSON("api/Entity1?$filter=Id eq '" + id + "'",
function (data) {
        ...
    });

my application encounter with the following error :

{"Message":"The query specified in the URI is not valid.","ExceptionMessage":"Type 'Entity 1' does not have a property 'Id'."}

Why the query specified in the URI is not valid using Web API OData ?

Upvotes: 3

Views: 5947

Answers (1)

Mohsen Alikhani
Mohsen Alikhani

Reputation: 1666

Adding Inheritance Support to OdataModelBuilder

Users can now define abstract entity types and entity types that derive from another entity type. OData doesn't support complex type inheritance.

This commit only adds support in the ModelBuilder. Support for inheritance in the ODataConventionModelBuilder, ODataMediaTypeFormatter and Query support is still pending.

Reference : http://aspnetwebstack.codeplex.com/SourceControl/changeset/f4c252a30e68

To solving above problem, use the following steps:

  1. Install nuget package from http://www.nuget.org/packages/Microsoft.AspNet.WebApi.OData
  2. Change type of Id property to string(It's because object type is not a supported type in OData. You should make your Id as odata primitive types like string).
  3. Remove any configuration from Application_Start.
  4. Add config.EnableQuerySupport() to WebApi.config.

Upvotes: 2

Related Questions