• <small id='mmsps'></small><noframes id='mmsps'>

    <i id='mmsps'><tr id='mmsps'><dt id='mmsps'><q id='mmsps'><span id='mmsps'><b id='mmsps'><form id='mmsps'><ins id='mmsps'></ins><ul id='mmsps'></ul><sub id='mmsps'></sub></form><legend id='mmsps'></legend><bdo id='mmsps'><pre id='mmsps'><center id='mmsps'></center></pre></bdo></b><th id='mmsps'></th></span></q></dt></tr></i><div id='mmsps'><tfoot id='mmsps'></tfoot><dl id='mmsps'><fieldset id='mmsps'></fieldset></dl></div>

      1. <legend id='mmsps'><style id='mmsps'><dir id='mmsps'><q id='mmsps'></q></dir></style></legend>
          <bdo id='mmsps'></bdo><ul id='mmsps'></ul>
        <tfoot id='mmsps'></tfoot>

        获取 Request.Headers 值

        时间:2023-10-25
        • <small id='josWu'></small><noframes id='josWu'>

        • <i id='josWu'><tr id='josWu'><dt id='josWu'><q id='josWu'><span id='josWu'><b id='josWu'><form id='josWu'><ins id='josWu'></ins><ul id='josWu'></ul><sub id='josWu'></sub></form><legend id='josWu'></legend><bdo id='josWu'><pre id='josWu'><center id='josWu'></center></pre></bdo></b><th id='josWu'></th></span></q></dt></tr></i><div id='josWu'><tfoot id='josWu'></tfoot><dl id='josWu'><fieldset id='josWu'></fieldset></dl></div>

            <tbody id='josWu'></tbody>

            <bdo id='josWu'></bdo><ul id='josWu'></ul>
          • <tfoot id='josWu'></tfoot>

                <legend id='josWu'><style id='josWu'><dir id='josWu'><q id='josWu'></q></dir></style></legend>

                  本文介绍了获取 Request.Headers 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我敢肯定,这很简单,但把我逼疯了!我在 Web 应用程序中使用了一个组件,该组件在 Web 请求期间通过添加标头XYZComponent=true"来标识自己 - 我遇到的问题是,您如何在视图中检查这一点?

                  Very simple I'm sure, but driving me up the wall! There is a component that I use in my web application that identifies itself during a web request by adding the header "XYZComponent=true" - the problem I'm having is, how do you check for this in your view?

                  以下方法不起作用:

                  if (Request.Headers["XYZComponent"].Count() > 0)
                  

                  也不是这样:

                  if (Request.Headers.AllKeys.Where(k => k == "XYZComponent").Count() > 0)
                  

                  如果尚未设置标头变量,则两者都会引发异常.任何帮助将不胜感激.

                  Both throw exceptions if the header variable has not been set. Any help would be most appreciated.

                  推荐答案

                  if (Request.Headers["XYZComponent"].Count() > 0)
                  

                  ... 将尝试计算返回字符串中的字符数,但如果标头不存在,它将返回 NULL,因此它抛出异常.您的第二个示例有效地执行了相同的操作,它将搜索 Headers 的集合,如果不存在则返回 NULL,然后您尝试计算其上的字符数:

                  ... will attempted to count the number of characters in the returned string, but if the header doesn't exist it will return NULL, hence why it's throwing an exception. Your second example effectively does the same thing, it will search through the collection of Headers and return NULL if it doesn't exist, which you then attempt to count the number of characters on:

                  改用这个:

                  if(Request.Headers["XYZComponent"] != null)
                  

                  或者,如果您想将空白或空字符串视为未设置,请使用:

                  Or if you want to treat blank or empty strings as not set then use:

                  if((Request.Headers["XYZComponent"] ?? "").Trim().Length > 0)
                  

                  空合并运算符 ??如果标头为空,将返回一个空白字符串,停止它抛出 NullReferenceException.

                  The Null Coalesce operator ?? will return a blank string if the header is null, stopping it throwing a NullReferenceException.

                  您的第二次尝试的变体也将起作用:

                  A variation of your second attempt will also work:

                  if (Request.Headers.AllKeys.Any(k => string.Equals(k, "XYZComponent")))
                  

                  <小时>

                  很抱歉没有意识到您正在明确检查值 true:


                  Sorry didn't realise you were explicitly checking for the value true:

                  bool isSet = Boolean.TryParse(Request.Headers["XYZComponent"], out isSet) && isSet;
                  

                  如果 Header 值为 false,或者 Header 尚未设置,或者 Header 是 true 或 false 以外的任何其他值,则将返回 false.会返回true就是Header值是字符串'true'

                  Will return false if Header value is false, or if Header has not been set or if Header is any other value other than true or false. Will return true is the Header value is the string 'true'

                  这篇关于获取 Request.Headers 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:HttpClient 请求的自定义标头 下一篇:如何向 WCF 服务添加跨域支持

                  相关文章

                  <tfoot id='w48F5'></tfoot>

                • <legend id='w48F5'><style id='w48F5'><dir id='w48F5'><q id='w48F5'></q></dir></style></legend>

                  <small id='w48F5'></small><noframes id='w48F5'>

                  <i id='w48F5'><tr id='w48F5'><dt id='w48F5'><q id='w48F5'><span id='w48F5'><b id='w48F5'><form id='w48F5'><ins id='w48F5'></ins><ul id='w48F5'></ul><sub id='w48F5'></sub></form><legend id='w48F5'></legend><bdo id='w48F5'><pre id='w48F5'><center id='w48F5'></center></pre></bdo></b><th id='w48F5'></th></span></q></dt></tr></i><div id='w48F5'><tfoot id='w48F5'></tfoot><dl id='w48F5'><fieldset id='w48F5'></fieldset></dl></div>

                      • <bdo id='w48F5'></bdo><ul id='w48F5'></ul>