The ASP. NET Web API uses OData

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.