Quantcast
Channel: ASPNETWebStack Issue Tracker Rss Feed
Viewing all articles
Browse latest Browse all 7215

Created Issue: Making it easy to extract key from OData URL [857]

$
0
0
The $link scenario requires user to parse the key from the url to proceed. However, we currently don't have easy way to do that. The workaround in the ODataServiceSample using following code to achieve it:

/// <summary>
/// Helper method to get the odata path for an arbitrary odata uri.
/// </summary>
/// <param name="request">The request instance in current context</param>
/// <param name="uri">OData uri</param>
/// <returns>The parsed odata path</returns>
public static ODataPath CreateODataPath(this HttpRequestMessage request, Uri uri)
{
if (uri == null)
{
throw new ArgumentNullException("uri");
}

var newRequest = new HttpRequestMessage(HttpMethod.Get, uri);
var route = request.GetRouteData().Route;

var newRoute = new HttpRoute(
route.RouteTemplate,
new HttpRouteValueDictionary(route.Defaults),
new HttpRouteValueDictionary(route.Constraints),
new HttpRouteValueDictionary(route.DataTokens),
route.Handler);
var routeData = newRoute.GetRouteData(request.GetConfiguration().VirtualPathRoot, newRequest);
if (routeData == null)
{
throw new InvalidOperationException("The link is not a valid odata link.");
}

return newRequest.GetODataPath();
}

/// <summary>
/// Helper method to get the key value from a uri.
/// Usually used by $link action to extract the key value from the url in body.
/// </summary>
/// <typeparam name="TKey">The type of the key</typeparam>
/// <param name="request">The request instance in current context</param>
/// <param name="uri">OData uri that contains the key value</param>
/// <returns>The key value</returns>
public static TKey GetKeyValue<TKey>(this HttpRequestMessage request, Uri uri)
{
if (uri == null)
{
throw new ArgumentNullException("uri");
}

//get the odata path Ex: ~/entityset/key/$links/navigation
var odataPath = request.CreateODataPath(uri);
var keySegment = odataPath.Segments.OfType<KeyValuePathSegment>().FirstOrDefault();
if (keySegment == null)
{
throw new InvalidOperationException("The link does not contain a key.");
}

var value = ODataUriUtils.ConvertFromUriLiteral(keySegment.Value, Microsoft.Data.OData.ODataVersion.V3);
return (TKey)value;
}

However, it is a hacky way to build the route on the fly and get route without the request pipeline. We should consider if there is any better way for this scenario.


Viewing all articles
Browse latest Browse all 7215

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>