Routing configuration
the Routeprefix route prefix must contain an OData string, otherwise the OData controller will not be routed.
V1 represents a version that can be versioned in this way, or otherwise.
Config. Count (). Filter (). (). Expand (). Select (). Maxtop (NULL); This configuration enables The Odata operator is available ( The old version of the Web Api2 does not have to be set).
Public Static classwebapiconfig{ Public Static voidRegister (httpconfiguration config) {//OData RouteCONFIG. Mapodataserviceroute (routeName:"v1odatarouteversioning", Routeprefix:"odata/v1", Model:getedmmodel ()); Config. Count (). Filter (). (). Expand (). Select (). Maxtop (NULL); Config. Addodataqueryfilter (); }}
EDM model Configuration
Entity name is Collection, the controller name is collectionsv1controller,action named Addcollection, andcollectionbm is the method parameter name. function name is called getcollection,function return data type is collectiondto,function The parameter name is userId. Use odataconventionmodelbuilder.namespace to define the namespace, which is part of the request URI.
Public classCollectionsv1controller:odatacontroller {[Enablequery] [HttpGet] PublicList<collectiondto> GetCollection (intuserId) { returncollectionbll.getcollection (userId); } [HttpPost] Public intaddcollection (Collectionbindingmodel collectionbm) {returncollectionbll.addcollection (COLLECTIONBM); }}Private StaticIedmmodel Getedmmodel () {Odataconventionmodelbuilder builder=NewOdataconventionmodelbuilder (); varCollectionset = Builder. Entityset<collection> ("Collections"). Entitytype.collection; varGetcollectionfunction = Collectionset.function ("GetCollection"). Returns<collectiondto>(); Getcollectionfunction.parameter<int> ("userId"); Collectionset.action ("addcollection"). returns<int> (). Parameter<collectionbindingmodel> ("COLLECTIONBM"); varDeletecollectionfunction = Collectionset.function ("deletecollection"). returns<int>(); Deletecollectionfunction.parameter<int> ("Collectionuserid"); Builder. Namespace="Service"; returnBuilder. Getedmmodel ();}
Controller and Controller methods
Controller inherits from Odatacontroller,odatacontroller on [apiexplorersettings (Ignoreapi = True)]
, [Odataformatting] , [Odatarouting] these three characteristics, as a result of [Apiexplorersettings (Ignoreapi = True)]
the impact, in The System.Web.Http.Description.ApiDescription instance does not contain a controller that inherits from Odatacontroller, and the Controller method uses [Enablequery] The OData operator can only be supported after decoration .
Custom Methods There are two ways to use the Entitycollectionconfiguration<tentitytype>. Action or entitycollectionconfiguration<tentitytype>. Function.
Use Entitycollectionconfiguration<tentitytype>. the Controller method defined by the Action can be passed through the body body;
Use Entitycollectionconfiguration<tentitytype>. the Controller method defined by the Function can be passed through the URL , but the URL notation is noteworthy, for example: http://localhost/HY_WebApi/Odata/V1/Collections/ Service.getcollection (userid=3), note that parentheses are used here to enclose the argument instead of "? userid=3 "This format
Use Action, HttpGet mode is not supported, only HttpPost mode is supported.
function only supports HttpGet mode and does not support HttpPost mode.
The ASP. NET Web API uses OData