【问题标题】:Why can't I get the headers in Web Api?为什么我无法在 Web Api 中获取标头?
【发布时间】:2014-03-09 03:06:43
【问题描述】:

我搜索了很多,无法弄清楚我错过了什么。

我正在尝试在 ajax 调用中发送标头:

        $.ajax({
            type: "GET",
            url: url,
            dataType: 'jsonp',
            headers: { "Authorization": "98765", "X-My-Secret-Token": "WhyCantIGetThis", "JustAnotherTest": "314987" },
            //beforeSend: function (request) {
            //    request.setRequestHeader("Authorization", "98765"); -> I tried this way too
            //},
            success: function (data) {
                $('#value1').html(data);
            }
        })

然后把它放进去:

       public override void OnActionExecuting(HttpActionContext actionContext)
       {
            var header_X_My_Secret_Token = actionContext.Request.Headers.SingleOrDefault(x => x.Key == "X-My-Secret-Token");
            var header_Authorization = actionContext.Request.Headers.SingleOrDefault(x => x.Key == "Authorization");
            var header_JustAnotherTest = actionContext.Request.Headers.SingleOrDefault(x => x.Key == "JustAnotherTest");

            var heeder_Authorization2 = actionContext.Request.Headers.Authorization;
        }

但这都是空的。谁能告诉我为什么?谢谢。 [编辑]

我可以在同一个域 ajax 调用中获取标题。但我也需要在跨域中获取它。怎么做?谢谢

【问题讨论】:

  • 我遇到了同样的问题,你解决了吗?

标签: c# asp.net-web-api jsonp


【解决方案1】:

你可以试试这个,在我的情况下它可以工作。

IEnumerable<string> values = new List<string>();
actionContext.Request.Headers.TryGetValues("Authorization", out values);

【讨论】:

    【解决方案2】:

    在您可以在跨域请求中发送标头之前,chrome 将使用 CORS 来确定可以发送哪些标头。要使浏览器能够发送您想要的标头,您可以向控制器添加一个 OPTIONS 方法。

       public HttpResponseMessage Options()
        {
    
            var response = new HttpResponseMessage();
            response.Headers.Add("Access-Control-Allow-Headers", Request.Headers.GetValues("Access-Control-Request-Headers").FirstOrDefault() );
            response.Headers.Add("Access-Control-Allow-Origin", "*");
            response.Headers.Add("Access-Control-Allow-Method", "GET");
            return response;
         }
    

    【讨论】:

      【解决方案3】:

      对于仍在寻找答案的任何人...

      我只需要启用 CORS:

      安装包 Microsoft.AspNet.WebApi.Cors

      using System.Web.Http;
      namespace WebService
      {
          public static class WebApiConfig
          {
              public static void Register(HttpConfiguration config)
              {
                  EnableCors(); \\<==============
      
                  config.Routes.MapHttpRoute(
                      name: "DefaultApi",
                      routeTemplate: "api/{controller}/{id}",
                      defaults: new { id = RouteParameter.Optional }
                  );
              }
          }
      }
      

      http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api

      【讨论】:

        猜你喜欢
        • 2019-04-03
        • 2017-07-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-28
        • 2020-12-14
        • 2021-05-04
        相关资源
        最近更新 更多