Simple Code to Setup WebApi with OData
2014/5/311 min read
bookmark this
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();
}
}