4.22 Use HttpRequestMessage Extension Methods
In Microsoft.AspNet.OData, set of HttpRequestMessage extension methods are provided through HttpRequestMessageExtensions. For services that don’t use LINQ or ODataQueryOptions.ApplyTo(), those extension methods can offer lots of help.
In Microsoft.AspNetCore.OData, which supports ASP.NET Core, set of HttpRequest extension methods are also provided through HttpRequestExtensions.
They are pretty much symmetrical, and the differences will be noted in the examples below. For further details, please refer to Microsoft.AspNet.OData.Extensions.HttpRequestMessageExtensions
and Microsoft.AspNet.OData.Extensions.HttpRequestExtensions
.
ODataProperties/IODataFeature
OData methods and properties can be GET/SET through:
- ODataProperties (for Microsoft.AspNet.OData) from httpRequestMessage.ODataProperties()
- IODataFeature (for Microsoft.AspNetCore.OData) from httpRequest.ODataFeature()
Each of them includes the followings:
Path The ODataPath of the request.
PathHandler Return DefaultODataPathHandler by default.
RouteName The Route name for generating OData links.
SelectExpandClause The parsed the OData SelectExpandClause of the request.
NextLink Next page link of the results, can be set through GetNextPageLink.
For example, we may need generate service root when querying ref link of a navigation property.
GetModel
For Microsoft.AspNet.OData only, get the EDM model associated with the request.
GetNextPageLink
Create a link for the next page of results, can be used as the value of @odata.nextLink
.
For example, the request Url is http://localhost/Customers/?$select=Name
.
Then the nextlink generated is http://localhost/Customers/?$select=Name&$skip=10
.
GetETag
Get the etag for the given request.
CreateErrorResponse
For Microsoft.AspNet.OData only, create a HttpResponseMessage to represent an error.
Then payload would be like:
{
"error":{
"code":"36",
"message":"Bad stuff",
"innererror":{
"message":"Exception message",
"type":"",
"stacktrace":""
}
}