Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WebAPI.NETCore2请求检查请求';s标头不是空的_C#_Asp.net Web Api_Asp.net Core Webapi_Httpresponse - Fatal编程技术网

C# WebAPI.NETCore2请求检查请求';s标头不是空的

C# WebAPI.NETCore2请求检查请求';s标头不是空的,c#,asp.net-web-api,asp.net-core-webapi,httpresponse,C#,Asp.net Web Api,Asp.net Core Webapi,Httpresponse,如何验证请求的页眉some header是否与book的bookId匹配 public IActionResult GetBooks() { // if 'some-header' value is empty, null , whitespace or request contains multiple ' some-header' headers then it should return UnauthorizedResult(); // if

如何验证请求的页眉some header是否与book的bookId匹配

    public IActionResult GetBooks()
    {
        // if 'some-header' value is empty, null , whitespace or request contains multiple ' some-header' headers then it should return UnauthorizedResult();
       // if 'some-header' is not above then it needs to be read from repository
    }
    public class Book 
    {
     public string bookId {get; set;}
    }

Request.Headers.GetValues()
将返回一个与HTTP请求中的头相对应的
IEnumerable
,然后您可以验证它是否包含多个值,或者它是否仅一次检查是否为null或空白(包括空)


Request.Headers.GetValues()
将返回一个与HTTP请求中的头相对应的
IEnumerable
,然后您可以验证它是否包含多个值,或者它是否仅一次检查是否为null或空白(包括空)


“IHeaderDictionary”不包含“GetValues”的定义,并且找不到接受“IHeaderDictionary”类型的第一个参数的可访问扩展方法“GetValues”(是否缺少using指令或程序集引用?)。我必须添加任何特定引用吗?@Lauren显然did方法不存在我用正确的方法更改了代码无法从“out System.Collections.Generic.IEnumerable”转换为“out Microsoft.Extensions.Primitives.StringValues”@Lauren检查我的新编辑,抱歉浪费了你一点时间,我应该先测试一下,然后再发布!应该是
返回新的UnauthorizedResult()?“IHeaderDictionary”不包含“GetValues”的定义,并且找不到接受类型为“IHeaderDictionary”的第一个参数的可访问扩展方法“GetValues”(是否缺少using指令或程序集引用)?我正在使用.net core 2。我必须添加任何特定引用吗?@Lauren显然did方法不存在我用正确的方法更改了代码无法从“out System.Collections.Generic.IEnumerable”转换为“out Microsoft.Extensions.Primitives.StringValues”@Lauren检查我的新编辑,抱歉浪费了你一点时间,我应该先测试一下,然后再发布!应该是
返回新的UnauthorizedResult()
Request.Headers.TryGetValue("some-header", out var headers);         
if(headers.Count > 1 || string.IsNullOrWhiteSpace(headers.FirstOrDefault())){
   return new UnauthorizedResult();
}