In the SOA world, the most important concept is contract ). In the world of cloud computing, the most important concepts about communication are also contracts. XML has a powerful ability to describe data. Atom format and atompub are both built on XML, which has become a standard under the impetus of Google and Microsoft. However, the real data interaction protocols such as atom/atompub and ODBC/oledb have a fundamental deficiency: the lack of specific descriptions of Data Types reduces the Interaction performance. Lack of control over data queries, such as the range of returned data sets or the paging capability. Microsoft has released odata Based on the EDM model. Here we can also see that the Entity Framework has different strategic considerations for ORM tools such as nhib.pdf.
In. in the early stage, remoting/WebService was used to process the communication between all programs. net 3.0 began to use WCF to unify the communication model, Asp. net mvc4 was launched to form a large one ASP. NET strategy, added webapi and singalr as communication services:
Odata is a Web protocol for querying and updating data. Odata uses Web technologies such as HTTP, Atom publish protocol (atompub), and JSON to provide access to different applications, services, and storage information. In addition to some basic operations (such as adding, deleting, modifying, and querying), some advanced operations are also provided, similar to the navigation of filtering data and objects. Odata extends the above protocols but does not replace them. It can be replaced by XML (atom) or JSON, but the important thing about odata is that it complies with the rest principle. In a sense, it is built on the 'simple' rest HTTP service and has a clear goal-to simplify and standardize the way we operate and query data. If you used to create search, filter, or paging APIs for your rest service, odata would be a good choice.
Let's take a look at the example of webapi enabling the odata protocol:
Http: // localhost: 8080/API/meetings
Http: // localhost: 8080/API/meetings? $ Filter = (leader EQ 'Mark nickls ')
Http: // localhost: 8080/API/meetings? $ Top = 2
Http: // localhost: 8080/API/meetings? $ Filter = meetingdate EQ datetime '2017-01-17 ′
Enable odata query in the project. First, add odata support for Web APIs to the project and use nuget to search for odata using ASP. NET Web APIs.
Microsoft. ASPnet. webapi. odata provides a series of classes to expand Web APIs.
Enable odata query in the project:
Public static void register (httpconfiguration config)
{
//...
Config. enablequerysupport ();
//...
}
If the self-hosting method is usedHttpselfhostconfigurationEnablequerysupport ():
VaR Config = new httpselfhostconfiguration (New uri ("http: // localhost: 8080 "));
Config. enablequerysupport ();
Then, change the return result of the action on the controls to iqueryable, and add the Label [queryable ()]:
// Get API/meeting [queryable (allowedqueryoptions = allowedqueryoptions. all)] public iqueryable <meeting> get () {return _ scheduledmeetings. asqueryable ();} needs to add using system. web. HTTP. odata. query; let's look at allowedqueryoptions to see which odata types are supported:
// Summary: // OData query options to allow for querying. [Flags] public enum AllowedQueryOptions { // Summary: // A value that corresponds to allowing no query options. None = 0, // // Summary: // A value that corresponds to allowing the $filter query option. Filter = 1, // // Summary: // A value that corresponds to allowing the $expand query option. Expand = 2, // // Summary: // A value that corresponds to allowing the $select query option. Select = 4, // // Summary: // A value that corresponds to allowing the $orderby query option. OrderBy = 8, // // Summary: // A value that corresponds to allowing the $top query option. Top = 16, // // Summary: // A value that corresponds to allowing the $skip query option. Skip = 32, // // Summary: // A value that corresponds to allowing the $inlinecount query option. InlineCount = 64, // // Summary: // A value that corresponds to the default query options supported by System.Web.Http.QueryableAttribute. Supported = 121, // // Summary: // A value that corresponds to allowing the $format query option. Format = 128, // // Summary: // A value that corresponds to allowing the $skiptoken query option. SkipToken = 256, // // Summary: // A value that corresponds to allowing all query options. All = 511, }
There is a format on it. We can format the output. Use the following code to format the format:
public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } );
Config. formatters. jsonformatter. addquerystringmapping ("$ format", "JSON", "application/JSON"); config. formatters. xmlformatter. addquerystringmapping ("$ format", "XML", "application/XML ");
config.EnableQuerySupport(); }
Note that odata queries are case sensitive.
I will use Fiddler to test this service
We did not write any special logic to support these functions, all provided by the framework. Does odata provide a good option for your search, filter, or paging API.
However, to publish queryable operations outside the organization, you can use query verification to add a protective layer to protect our services. Microsoft Program Manager Hongmei Ge introduced several scenarios for adding verification in queryable API.
The first scenario stated by Hongmei is to use the allowedqueryoptions attribute to only allow queries containing $ top and $ Skip. As follows:
[Queryable (allowedqueryoptions = allowedqueryoptions. Skip | allowedqueryoptions. Top)]
Public iqueryable get (INT projectid)
You can also use the maxtop and maxskip attributes to limit the maximum values of $ top and $ skip to 100 and 200:
[Queryable (maxtop = 100)]
Public iqueryable get (INT projectid)
[Queryable (maxskip = 200)]
Public iqueryable get (INT projectid)
With allowedorderbyproperties, you can sort the results by the ID attribute, because sorting by other attributes may be slow:
[Queryable (allowedorderbyproperties = "ID")]
Public iqueryable get (INT projectid)
If the client is allowed to use equal comparison in $ filter, use allowedlogicaloperators to verify the comparison:
[Queryable (allowedlogicaloperators = allowedlogicaloperators. Equal)]
Public iqueryable get (INT projectid)
Set allowedarithmeticoperators to none to disable the arithmetic operations in $ filter:
[Queryable (allowedarithmeticoperators = allowedarithmeticoperators. None)]
Public iqueryable get (INT projectid)
You can also use the allowedfunctions attribute to limit the usage of functions in $ filter:
[Queryable (allowedfunctions = allowedfunctions. startswith)]
Public iqueryable get (INT projectid)
The above code means that the startswith function can only be used in $ filter.
Hongmei also demonstrates query verification in advanced scenarios, such as $ Skip, $ top, $ orderby, $ filter custom default verification logic, and use odataqueryoptions to verify the query.
Related Articles:
Odata developers reference: http://www.odata.org/developers/
Odata in ASP. NET: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api
Limiting odata query options: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options
Odata security: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-security-guidance
Add an odata feed to your app using web API: http://marknic.net/2013/03/02/add-an-odata-feed-to-your-app-using-web-api/
Working with odata queries in ASP. NET web API: http://www.codeguru.com/csharp/.net/working-with-odata-queries-in-asp.net-web-api.htm
A new option for creating odata: web API: http://msdn.microsoft.com/zh-cn/magazine/dn201742.aspx
Sample Code download: http://files.cnblogs.com/shanyou/WebApiOData.zip