如果标头不存在,如何获取空值 [英] How to get a null value if the header doesn't exist
本文介绍了如果标头不存在,如何获取空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用请求上下文获取名为"token"的标头的值.
I am using the request context to get the value of the header called "token".
var token = context.request.Headers.GetValues("Token")
现在如果标题存在.这一切都可以工作数百个,但是现在如果标头不存在,我希望它返回null.但相反,它引发异常System.InvalidOperationExecption
Now If the header exists. This all works hundreds, But now if the header doesn't exist, I want it to return null. But instead it throws an exception System.InvalidOperationExecption
我唯一的选择就是抓住它吗?
Is my only option to throw a try catch around it?
推荐答案
您可以这样做
if (Context.Request.Headers["Token"] != null)
{
var token = Context.Request.Headers.GetValues("Token");
return token;
}
else
return null;
这篇关于如果标头不存在,如何获取空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文