Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OData Obsolete Code

Tags:

c#

odata

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?

like image 393
Pizzor2000 Avatar asked Sep 10 '26 04:09

Pizzor2000


1 Answers

@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.

like image 73
Sam Xu Avatar answered Sep 12 '26 18:09

Sam Xu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!