C# Asp.Net Web Api跟踪/导致odata查询失败

C# Asp.Net Web Api跟踪/导致odata查询失败,c#,asp.net-web-api,odata,C#,Asp.net Web Api,Odata,我正在使用HttpClient使用以下代码查询WebApi控制器: var uri = new Uri(url); var message = new HttpRequestMessage(method, uri); if (content != null) message.Content = new StringContent(content, Encoding.UTF8, "application/json"); var cl

我正在使用HttpClient使用以下代码查询WebApi控制器:

        var uri = new Uri(url);
        var message = new HttpRequestMessage(method, uri);
        if (content != null) message.Content = new StringContent(content, Encoding.UTF8, "application/json");

        var client = new HttpClient();
        if (content != null) Debug.WriteLine($"Payload:\n {content}");
        var response = await client.SendAsync(message);
        return response;
生成的Url如下所示: ?$filter=%20Name%20eq%20'Some%20Role'/

注意后面的“/”。这会导致OData验证(代码如下)失败:

[HttpGet]
[路线(“”)
公共异步任务GetRoles(ODataQueryOptions选项)
{
GuardForODataException(选项);
var filteredResults=await _roleService.GetRolesAsync(选项);
返回Ok(filteredResults);
}
公共void GuardForODataException(ODataQueryOptions选项)
{
尝试
{
var validationSettings=新的ODataValidationSettings();
选项。验证(验证设置);
}
捕获(例外e)
{
var结果=
新的ValidationFailedResult(
要求
新的ValidationResultModel(
新的验证结果(
新列表{new ValidationFailure(“,“Odata查询无效”)});
抛出新的HttpResponseException(result.ExecuteAsync(default(CancellationToken)).result);
}
}
我得到的例外是

“在位置9处需要一个标识符。”

但是,如果我删除了尾随的/,那么odata查询将返回预期的结果


我找不到太多关于如何在结尾没有/的情况下构造
HttpRequestMessage
。以前有人遇到过这种情况吗?

是时候进行一些断点调试了:D当您到达
var uri=new uri(url)时,
url
变量中有什么内容?一点上下文/提示
HttpRequestMessage
遵循
Uri
您可以准确地传递它。查询没有问题,我在baseUrl配置中有一个尾随“/”
    [HttpGet]
    [Route("")]
    public async Task<IHttpActionResult> GetRoles(ODataQueryOptions<RoleSummaryDto> options)
    {
        GuardForODataException(options);

        var filteredResults = await _roleService.GetRolesAsync(options);
        return Ok(filteredResults);

    }

    public void GuardForODataException<T>(ODataQueryOptions<T> options)
    {
        try
        {
            var validationSettings = new ODataValidationSettings();
            options.Validate(validationSettings);
        }
        catch (Exception e)
        {

            var result =
                new ValidationFailedResult(
                    Request,
                        new ValidationResultModel(
                            new ValidationResult(
                                new List<ValidationFailure> { new ValidationFailure("", "Odata Query is invalid") })));

            throw new HttpResponseException(result.ExecuteAsync(default(CancellationToken)).Result);
        }
    }