ASP.NET MVC Web API OData v4 – Initialize from Web Application Project

ASP.NET MVC Web API OData v4 – Initialize from Web Application Project

– Install / Update Microsoft.AspNet.OData v6.1.0 from NuGet.
– Update some Routing Setting from WebApiConfig.cs as follow.

 
   using System.Web.OData.Builder;
   using System.Web.OData.Extensions;
 
   public static class WebApiConfig
   {
     public static void Register(HttpConfiguration config)
     {
       ODataModelBuilder builder = new ODataConventionModelBuilder();
 
       /* Entity Framework – Model. */
       builder.EntitySet<Product>("Products");   /* Define the ODataController Class Name – ProductsController. */
       builder.EntitySet<… …>("… …");
       builder.EntitySet<… …>("… …");
       builder.EntitySet<… …>("… …");
       builder.EntitySet<… …>("… …");
       builder.EntitySet<… …>("… …");
       builder.EntitySet<… …>("… …");
       builder.EntitySet<… …>("… …");
       builder.EntitySet<… …>("… …");
 
       config.MapODataServiceRoute(
         routeName: "ODataRoute",
         routePrefix: null,
         model: builder.GetEdmModel());
     }
   }
 
   /*
     would return the metadata of OData in JSON Format.
     http://… …/
 
     would execute the Get Function from Product OData Controller. Bascially, it would return all records from Controller in JSON Format.
     http://… …/Products
   */
 
   Reference From MSDN
 

– Some Configuration Setting from web.conf needs to be added or to be updated as follow.

 
   <configuration>
 
     … …
 
     <runtime>
 
       <assemblyBinding … …>
 
         … …
 
         <dependentAssembly>
           <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
           <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
         </dependentAssembly>
 
         <dependentAssembly>
           <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
           <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
         </dependentAssembly>
 
       </assemblyBinding>
 
     </runtime>
 
   </configuration>
 

– Define OData Controller from ApiController to ODataController. ( Controller – Inheritance )
– Define the Relevant OData Controller Method from the Controller Code.
   ( Get ( Get All Items ) / Get with ID Parameter ( Get Item with Criteria ) / Post for Insert Record / Post for Update Record or something else. )
   ( Test & Validate the Returned Data Collection from Each Standard of OData URL Parameter
     ( Filter / Contain / Sorting / ?? ) from Search Result and Debug Mode. )
 
Some Standard of OData URL Parameter.