【问题标题】:Cors, Web Api, IE8, Post Complex DataCors、Web Api、IE8、发布复杂数据
【发布时间】:2013-09-30 02:39:17
【问题描述】:

作为我工作环境的一部分,我们需要支持 IE8,但希望推进技术,特别是 CORS。

我在 ie8 中将复杂对象发布到 cors 服务时遇到问题。对象为空。以下是重现的步骤。如果需要,我可以将项目上传到 github。

我创建了一个新的 mvc4 项目。添加了一个 API 控制器。并进行了以下更改。

支持预检复杂 cors 调用 (global.asax):

    protected void Application_BeginRequest()
    {
        //This is needed for the preflight message
        //https://stackoverflow.com/questions/13624386/handling-cors-preflight-requests-to-asp-net-mvc-actions
        if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS")  {  Response.Flush(); }
    }

来源:Handling CORS Preflight requests to ASP.NET MVC actions

支持 text/plain(ie8 只发送带有 cors 的 text/plain)(global.asax):

    protected void Application_Start()
    {
        //This is needed to support text/plain
        HttpConfiguration config = GlobalConfiguration.Configuration;
        config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain"));
        config.Formatters.Remove(config.Formatters.FormUrlEncodedFormatter);
        config.Formatters.Remove(config.Formatters.XmlFormatter); 

        ...
    }

信用:Posting text/plain as a complex object in WebAPI with CORS

支持除动词以外的其他函数名称(put/post/etc)(WebApiConfig.cs)”

    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "APICustom",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        ...
    }

支持 cors (web.config)

<httpProtocol>
   <customHeaders>
     <!-- cors -->
     <add name="Access-Control-Allow-Origin" value="*" />
     <add name="Access-Control-Allow-Headers" value="Content-Type" />
   </customHeaders>
</httpProtocol>

API控制器,我叫PersonController.cs

 public class PersonController : ApiController
{

    public List<string> Get()
    {
        List<string> s = new List<string>();
        s.Add("s");
        s.Add("t");
        s.Add("u");
        return s;
    }



    [Serializable()]
    public class BaseReply
    {
        public bool successful = true;
        public string error;
    }
    [Serializable()]
    public class UpdateSomethingReply:  BaseReply
    {
        public UpdateSomethingRequest request;
        public List<string> stuff = new List<string>();
    }
    [Serializable()]
    public class UpdateSomethingRequest
    {
        public int hasInt;
        public string hasString;
    }
    //[FromBody] 
    [HttpPost]
    public UpdateSomethingReply UpdateSomething([FromBody] UpdateSomethingRequest request)
    {
        string body = Request.Content.ReadAsStringAsync().Result;
        UpdateSomethingReply reply = new UpdateSomethingReply();
        reply.request = request;

        reply.stuff.Add("v");
        reply.stuff.Add("w");
        reply.stuff.Add("x");
        return reply;
    }

这就是服务上的变化程度。所以接下来我创建一个客户端。这也是一个mvc4项目。这里有很基本的东西。

用 cors (index.cshtml) 填充 ie8:

<script src="~/Scripts/jQuery.XDomainRequest.js"></script>

来源:https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest

调用 cors 服务

 $(document).ready(function () {
        $.when(
          $.ajax({
              url: urls.person.UpdateSomething,
              type: 'post',
              contentType: "application/json; charset=utf-8",
              dataType: 'json',
              data: JSON.stringify({
                  hasInt: 1,
                  hasString: "u"
              })
          })
        )
        .fail(function (jqXHR, textStatus, errorThrown) {
        })
        .done(function (data) {
            console.log(JSON.stringify(data));
        });

        $.when(
          $.ajax({
              url: urls.person.Get,
              dataType: 'json'
          })
        )
        .fail(function (jqXHR, textStatus, errorThrown) {
        })
        .done(function (data) {
            console.log(JSON.stringify(data));
        });

        $.when(
          $.ajax({
              url: urls.person.UpdateSomething,
              type: 'post',
              contentType: "text/plain",
              dataType: 'json',
              data: JSON.stringify({
                  hasInt: 1,
                  hasString: "u"
              })
          })
        )
        .fail(function (jqXHR, textStatus, errorThrown) {
        })
        .done(function (data) {
            console.log(JSON.stringify(data));
        });
    });

正如我之前所说,所有 3 个调用都在 ie8 中完成。但是服务中的请求对象在 ie8 中为空,在 Firefox 中它被填充,即使我强制内容类型为 text/plain

IE8 控制台输出:

{"request":null,"stuff":["v","w","x"],"successful":true,"error":null}

Firefox 控制台输出:

{"request":{"hasInt":1,"hasString":"u"},"stuff":["v","w","x"],"successful":true,"error":null}

2013 年 9 月 25 日更新

我可以确认正文正在发送,但没有被 web api 解析。如果我添加以下 hack,它将按预期返回数据。在 Firefox 中,正文将为空,请求对象已填充。在ie8中body仍然包含内容,请求为null。

    [HttpPost]
    public UpdateSomethingReply UpdateSomething(UpdateSomethingRequest request)
    {
        if (request == null && Request.Content.ReadAsStringAsync().Result !="")
        {
            request = JsonConvert.DeserializeObject<UpdateSomethingRequest>(Request.Content.ReadAsStringAsync().Result);
       }

        UpdateSomethingReply reply = new UpdateSomethingReply();
        reply.request = request;
        reply.body=Request.Content.ReadAsStringAsync().Result;
        reply.headers = Request.Headers.ToString();
        reply.stuff.Add("v");
        reply.stuff.Add("w");
        reply.stuff.Add("x");
        return reply;
    }

【问题讨论】:

  • 让我想知道这是否是 jQuery 决定不对 IE 进行例外处理以使 CORS 请求在没有补丁的情况下在 IE 中工作的原因之一。
  • 请注意,在没有 Content-Type 标头的情况下,请求的推断内容类型不是 text/plain。根据规范,它是 application/octet-stream,尽管服务器可能会根据 URI 中包含的资源属性来确定其他方式,例如。我意识到 XDomainRequest 上的 MSDN 博客声称 Content-Type 仅限于 text/plain,但这是不正确的。邮件 RFC 说 Content-Type 默认为 text/plain,但 HTTP (RFC 2616) 说 application/octet-stream。
  • @kev- 当然,cors 对 ie8 有限制。但在这种情况下,我认为它很好,相反,我认为它的 web api 就是问题所在,因为它返回数据,只是不解析请求
  • @ray 谢谢,我检查一下,但我认为它是作为文本出现的,因为如果我删除文本纯文本的全局 asax 代码会出错,但也许我只是在 Firefox 中对其进行了测试。我会验证
  • 由于缺少 Content-Type 标头,我不希望任何框架自动解析 XDomainRequest 发送的任何请求正文。

标签: jquery asp.net-mvc internet-explorer-8 asp.net-web-api cors


【解决方案1】:

这是我正在谈论的代码。将此创建为一个新类,我在我的 WebAPI 项目中创建了一个 DelegatingHandlers 文件夹(但话又说回来,我还有一个过滤器文件夹,一个模型绑定文件夹......)

我已经包含了大量可以轻松删除的 cmets。

以下假设 IE 8/9 将始终发送“JSON”数据。如果您的 webAPI 实现允许内容协商,并且您希望为 IE8/9 包含该功能,那么您显然需要在下面的代码中添加一些 if 语句,但这应该足以让您继续前进。我个人只是表示我只接受来自 IE 8/9 的 JSON。

namespace REDACTED.WebApi.DelegatingHandlers
{
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Threading;
    using System.Threading.Tasks;

    /// <summary>
    /// Gives the WebAPI the ability to handle XDomainRequest objects with embedded JSON data.
    /// </summary>
    public class XDomainRequestDelegatingHandler : DelegatingHandler
    {
        protected override Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request, CancellationToken cancellationToken)
        {
            // XDomainRequest objects set the Content Type to null, which is an unchangable setting.
            // Microsoft specification states that XDomainRequest always has a contenttype of text/plain, but the documentation is wrong.
            // Obviously, this breaks just about every specification, so it turns out the ONLY extensibility
            // point to handle this is before the request hits the WebAPI framework, as we do here.

            // To read an apology from the developer that created the XDomainRequest object, see here: 
            // http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx

            // By international specification, a null content type is supposed to result in application/octect-stream (spelling mistake?),
            // But since this is such an edge case, the WebAPI framework doesn't convert that for us before we hit this point.  It is unlikely, 
            // but possible that in a future Web.Api release, we will need to also sniff here for the octect header.
            if (request.Content.Headers.ContentType == null)
            {
                request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            }

            return base.SendAsync(request, cancellationToken);
        }
    }
}

我的 WebAPIConfig 文件如下所示:

        public static void Register(HttpConfiguration config)
        {
             // Normal config.Routes statements go here

            // Deserialize / Model Bind IE 8 and 9 Ajax Requests
            config.MessageHandlers.Add(new XDomainRequestDelegatingHandler());
        }

然后,为了确保我的 POST 调用符合 IE 8 和 9,我在我的 JS 中添加了以下内容(但显然,如果您还使用自己的 API,则只需要包含此内容)

esbPost: function (apiUrl, apiData, fOnSuccess, fOnFailure) {
    $.support.cors = true; // Not sure that I need this.

    var testModernAjax = function () {
        if (window.XMLHttpRequest) {
            var testRequest = new XMLHttpRequest;

            // IE 8 / 9 with jQuery can create XMLHttpRequest objects, but only modern 
            // CORS implementing browsers (everything + IE10) include the withCredentials specification.
            if ('withCredentials' in testRequest) {
                return true;
            }
            return false;
        }
        return false;
    };

    var testMsieAjax = function () {
        if (window.XDomainRequest) {
            return true;
        }
        return false;
    };

    //All browsers, and IE 10
    if (testModernAjax()) {
        $.ajax({
            url: apiUrl,
            type: 'POST',
            dataType: 'json',
            data: apiData,
            success: function (result) {
                if (fOnSuccess) {
                    fOnSuccess(result);
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                if (fOnFailure) {
                    fOnFailure(jqXHR, textStatus, errorThrown);
                }
            }
        });
    //IE 8 / 9
    } else if (testMsieAjax()) {
        var xdr = new XDomainRequest();
        xdr.onload = function () {
            var parsedResponse = $.parseJSON(xdr.responseText);
            if (fOnSuccess) {
                fOnSuccess(parsedResponse);
            }
        };
        xdr.onerror = function () {
            if (fOnFailure) {
                fOnFailure();
            }
        };
        xdr.onprogress = function () { };
        xdr.open("post", apiUrl);
        xdr.send(JSON.stringify(apiData));
    } else {
        // IE 7 can only do AJAX calls through a flash/iframe exploit, earlier do not include ajax support.
        throw new 'This browser is unsupported for this solution.';
    }
},

就我个人而言,我将 JSONP 用于 GET,而不使用 PUTS 或 DELETES,所以这对我来说已经足够了。如果我要重新做这个项目,我会使用 PUTS 和 DELETES。为了使 IE 8 / 9 处理跨域 PUTS 和 DELETES,其显然常见的做法是在正在发送的数据或标题中包含一个新节点,称为“Type”的某个变体,并使用字符串“PUT”或“DELETE” ”。不过我不确定我会从哪里闻出来。

启用 CORS 就像将以下内容放入 Web.Config 一样简单。

<system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <!--<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />-->
      </customHeaders>
    </httpProtocol>

正如您在上面的评论中看到的,您还可以通过发起 url(*)和请求类型(put、post 等)来限制 CORS。完全不需要stuff like this 完全。 This guy's blog gives a really good walkthrough.

这就是您需要对一个全新的 WebAPI 项目执行的所有操作,以使其同时支持 CORS 和 IE 8/9。

【讨论】:

【解决方案2】:

在我找到另一个解决方案或者我们可以停止支持 IE8 之前,这是公认的 hack。感谢一位同事提出这个建议。

  1. 在 global.asax 中移除对 text/plain 的支持,ie8 发送的 headers 全部为空。正如 cmets 中所讨论的,请求正文不会自动解析。内容保留在正文中。通常(比如在 Firefox 中)正文被解析为请求对象并替换为空字符串。
  2. 在 App_Start 中创建一个名为 GenericBinder 的类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    using Newtonsoft.Json;
    using System.Web.Http.Controllers;
    using System.Web.Http.ModelBinding;
    namespace Admin2
    {
      public class GenericBinder : IModelBinder
      {
        public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
        {
            bindingContext.Model = JsonConvert.DeserializeObject(actionContext.Request.Content.ReadAsStringAsync().Result, bindingContext.ModelType);
            return true;
        }
      }
    }
    
  3. 如下更改人员控制器

    using System.Web.Http.ModelBinding;
    
    ...
    
    [HttpPost]
    public UpdateSomethingReply UpdateSomething([ModelBinder(typeof(GenericBinder))] UpdateSomethingRequest request)
    {
      UpdateSomethingReply reply = new UpdateSomethingReply();
      reply.request = request;
      reply.stuff.Add("v");
      reply.stuff.Add("w");
      reply.stuff.Add("x");
      return reply;
    }
    

IE8 现在可以发送复杂的数据了。

【讨论】:

  • 我真的不想走这条路,因为我已经在我的代码中使用了 FromUri 和 FromBody 绑定器,并且为每个端点添加新的模型绑定类型似乎充满了困难。更糟糕的是,在您的示例代码中,没有办法说“如果请求指定了内容类型,则使用默认绑定”,因为 WebAPI 没有真正的默认绑定,只有一堆提供者工厂。
  • 相反,我最终做的是创建一个新的自定义委托处理程序,并嗅探缺少内容类型的传入请求。如果是这样,我将内容类型更新为我认为应该的内容,然后当请求到达 WebAPI 框架时,它会根据我设置的内容自动解析它。结果,我的其余代码甚至都不知道 IE 是个问题。 Dennis Puzak 有一个简单的例子:stackoverflow.com/questions/18964258/…
  • 您能否用代码发布您的答案,以便我尝试一下,并将其标记为已接受的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-06
  • 2016-08-09
  • 1970-01-01
  • 2015-04-22
相关资源
最近更新 更多