c# - How to safely access actionContext.Request.Headers.GetValues if the key is not found? -
i doing this, throws exception if key not found.
this snippet inside of web api filter inherits actionfilterattribute
, in overriden method onactionexecuting
.
if (actioncontext.request.headers.getvalues("some_key") != null && actioncontext.request.headers.getvalues("some_key").first() == "hello") { }
am forced wrap in try/catch?
class myfilter : system.web.http.filters.actionfilterattribute { public override void onactionexecuting(system.web.http.controllers.httpactioncontext actioncontext) { ienumerable<string> values; if (actioncontext.request.headers.trygetvalues("some_key", out values) && values.first() == "hello") { } } }
Comments
Post a Comment