gpt4 book ai didi

c# - "' HttpRequestMessageProperties ' does not contain a definition for ' PathHandler '"尽管使用 System.Web.Odata.Extensions

转载 作者:行者123 更新时间:2023-11-30 15:57:21 27 4
gpt4 key购买 nike

我目前正在学习如何使用 MS's ASP.NET web API guide 创建 OData 服务.在 Helpers 部分,尝试获取字段 HttpRequestMessage.ODataProperties().PathHandler...Model 会引发上述错误。根据MS's documentation , import System.Web.OData.Extensions 应该是对的。

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

相关代码,基本和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 也有同样的问题

最佳答案

对于出于与我相同的原因最终来到这里的其他人,这是我的代码(显然 keySegment 也发生了变化,Value 包含已经转换的对象):

public static TKey GetKeyFromUri<TKey>(HttpRequestMessage request, Uri uri)
{
if (uri == null)
{
throw new ArgumentNullException(nameof(uri));
}

var urlHelper = request.GetUrlHelper() ?? new UrlHelper(request);
var serviceRoot = urlHelper.CreateODataLink(request.ODataProperties().RouteName, request.GetPathHandler(), new List<ODataPathSegment>());
var odataPath = request.GetPathHandler().Parse(serviceRoot, uri.LocalPath, request.GetRequestContainer());

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

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

关于c# - "' HttpRequestMessageProperties ' does not contain a definition for ' PathHandler '"尽管使用 System.Web.Odata.Extensions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45106341/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com