HttpRequestMessageProperties‘不包含’PathHandler‘的定义“,尽管使用了System.Web.Odata.Extensions

我目前正在学习如何使用MS's ASP.NET web API guide 创建OData服务。在“帮助器”部分中,尝试获取字段HttpRequestMessage.ODataProperties().PathHandler...Model会抛出上述错误。根据MS's documentation的说法,导入System.Web.OData.Extensions应该是正确的。

使用Odata.Client的6.13.0和Odata.Core的7.0.0。

相关代码,基本上与web api指南1:1:

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

namespace WebApiGuide {
public static class Helpers
{
    public static TKey GetKeyFromUri<TKey>(HttpRequestMessage Request, Uri uri)
    {
        if (uri == null)
        {
            throw new ArgumentNullException("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;
    }
}
}

编辑:如果它使问题更加具体,我在System.Web.OData.Routing中的KeyValuePathSegment也有同样的问题

转载请注明出处:http://www.chufzs.com/article/20230526/2057893.html