Pizzor2000
Pizzor2000

Reputation: 379

OData Obsolete Code

I am trying to follow along this tutorial to create an OData service. I am looking at this topic about navigation properties:

https://learn.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/entity-relations-in-odata-v4

It appears some of this code is obsolete (the article is from 2014, but I'm using Visual Studio 2017).

I have quite a few red underlines on my Helper class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web.Http.Routing;
using System.Web.OData.Extensions;
using System.Web.OData.Routing;
using Microsoft.OData;
using Microsoft.OData.UriParser;

namespace ProductService
{
    public static class Helpers
    {
        public static TKey GetFromUri<TKey>(HttpRequestMessage request, Uri uri)
        {
            if(uri == null)
                throw new ArgumentException("uri");

            var urlHelper = request.GetUrlHelper() ?? new UrlHelper(request);

            string serviceRoot = urlHelper.CreateODataLink(
            request.ODataProperties().RouteName,
            request.ODataProperties().PathHandler, new List<ODataPathSegment>());

            var odataPath = request.ODataProperties().PathHandler.Parse(
                request.ODataProperties().Model,
                serviceRoot, uri.LocalPath);

            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,
                ODataVersion.V4);
            return (TKey)value;
        }
    }
}

I have problems with three pieces of code on this class:

request.ODataProperties().PathHandler

and

request.ODataProperties().Model

I get errors:

'HttpRequestMessageProperties' does not contain a definition for 'PathHandler' and no extension method...

It is also unable to find the KeyValuePathSegment class.

Is there a way to rewrite this class to keep it current?

Upvotes: 3

Views: 1269

Answers (2)

Andriy Lisovoy
Andriy Lisovoy

Reputation: 111

Hoping that it might save some time I post here the same piece of code corrected with the new API.

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNet.OData.Extensions;
using Microsoft.AspNetCore.Http;
using Microsoft.OData;
using Microsoft.OData.UriParser;

namespace ProductService
{
    
    public static class Helpers
    {
        public static TKey GetKeyFromUri<TKey>(HttpRequest request, Uri uri)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }

            var oDataPathHandler = request.GetPathHandler();
            var oDataLink = request.GetUrlHelper().CreateODataLink(request.ODataFeature().RouteName, oDataPathHandler, new List<ODataPathSegment>());
            var odataPath = oDataPathHandler.Parse(oDataLink, uri.AbsoluteUri, request.GetRequestContainer());

            var keySegment = odataPath.Segments.OfType<KeySegment>().FirstOrDefault();
            if (keySegment?.Keys == null || !keySegment.Keys.Any())
            {
                throw new InvalidOperationException("The link does not contain a key.");
            }

            return (TKey)keySegment.Keys.FirstOrDefault().Value;
        }
    }
}

Upvotes: 1

Sam Xu
Sam Xu

Reputation: 3380

@Pizzor2000

Some breaking changes introduced in Web API OData library from 5.x to 6.x version. All the changes you can find from release note at :https://github.com/OData/WebApi/releases/tag/v6.0.0

for your examples:

you can call extension methods to get the original properties, for example:

https://github.com/OData/WebApi/blob/master/src/System.Web.OData/Extensions/HttpRequestMessageExtensions.cs#L307 to get the IEdmModel.

https://github.com/OData/WebApi/blob/master/src/System.Web.OData/Extensions/HttpRequestMessageExtensions.cs#L352 to get the PathHandler.

Besides, KeyValuePathSegment is removed, Web API OData uses the https://github.com/OData/odata.net/blob/master/src/Microsoft.OData.Core/UriParser/SemanticAst/KeySegment.cs#L22 instead.

Hope it can help you.

Upvotes: 4

Related Questions