How to get a null value if the header doesn't exist
问题 I am using the request context to get the value of the header called "token". var token = context.request.Headers.GetValues("Token") 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? 回答1: you can do this if (Context.Request.Headers["Token"] != null) { var token = Context.Request.Headers.GetValues("Token"); return