WebApi + OData setup

6/1/2014·1 min read

This section show how to setup OData Web api

Register WebApi at Application start


protected void Application_Start()
{
    GlobalConfiguration.Configure(WebApiConfig.Register);
}

Register OData


 public static void Register(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();
         
 ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet("YourEntityClassOrYourModelClassName");
            config.Routes.MapODataRoute("ODataRoute", "/url/forYourOData", builder.GetEdmModel());
        }

Define Odata Api Controller


public class YourEntityClassOrYourModelClassController : ODataController
{
        [Queryable]
        public IQueryable GetYourEntityClassOrYourModelClass()
        {
            return ReturnLogicForTheMethod().AsQueryable();
        }
}