OData 废弃代码

3

我正在尝试按照这个教程创建一个OData服务。我正在查看关于导航属性的主题:

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

看起来这段代码有些过时了(文章出自2014年,但我正在使用Visual Studio 2017)。

我的Helper类上有很多红色下划线:

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;
        }
    }
}

我在这个类中有三段代码存在问题:
request.ODataProperties().PathHandler

并且

request.ODataProperties().Model

我遇到了错误:

'HttpRequestMessageProperties' 没有包含 'PathHandler' 的定义,也没有扩展方法...

它也无法找到 KeyValuePathSegment 类。

是否有一种重写这个类以使其保持最新的方法?

2个回答

4
从Web API OData库的5.x版本到6.x版本引入了一些重大变化。 你可以在发行说明中找到所有更改:https://github.com/OData/WebApi/releases/tag/v6.0.0
对于你的示例:
您可以调用扩展方法来获取原始属性,例如:
使用https://github.com/OData/WebApi/blob/master/src/System.Web.OData/Extensions/HttpRequestMessageExtensions.cs#L307来获取IEdmModel
使用https://github.com/OData/WebApi/blob/master/src/System.Web.OData/Extensions/HttpRequestMessageExtensions.cs#L352来获取PathHandler
此外,KeyValuePathSegment已被删除,Web API OData现在使用https://github.com/OData/odata.net/blob/master/src/Microsoft.OData.Core/UriParser/SemanticAst/KeySegment.cs#L22
希望这可以帮助您。

1
希望这能节省一些时间,我在这里发布同样的代码片段,已经使用了新的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;
        }
    }
}

HttpRequestMessage需要System.Net.Http。所以我添加了System.Net.Http。然后,IDE报错ODataFeatures()不存在。经过一些搜索,我发现可以使用Microsoft.AspNetCore.Http。但是,由于我的项目针对的是.NET Framework而不是.NET Core,因此无法安装该包。经过更多的搜索,我将ODataFeatures替换为ODataProperties。目前没有编译错误了。我会继续更新情况。 - Andes Lam
是的,根据此链接https://learn.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/entity-relations-in-odata-v4#creating-a-relationship-between-entities,它按预期工作。 总之,在您的代码中我添加了以下内容: 1.使用System.Net.Http 2.将ODataFeature()替换为ODataProperties() - Andes Lam

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接