我尝试获取如下所示的 header 值 -
IEnumerable<string> headerValues = request.Headers.GetValues("MyCustomerId");
var id = headerValues.FirstOrDefault();
如果 header 值为 null 或不存在,则抛出错误 - InvalidOperationException
GetValues
的 null 检查不提供任何值,因为它永远不会返回 null。如果 header 不存在,您将得到一个 InvalidOperationException
有什么技巧吗?
最佳答案
您可以像这样检查是否为 null:
if(System.Web.HttpContext.Current.Request.Headers["MyCustomerId"] != null)
{
// do something
}
久经考验
关于c# - 无法检查请求中 header 值的空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35452709/