加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 使用CORS和IE9(XDomainRequest对象)的ASP Web AP

发布时间:2020-12-16 07:29:01 所属栏目:asp.Net 来源:网络整理
导读:我一直在疯狂,试图让 jquery.ajax与ie9一起工作.所以我有一个实现CORS的ASP Web API 2 Rest API.所有浏览器的CORS请求都有效. IE9没有用,因为它使用XDomainRequest.我设法通过为IE9自定义实现ajaxTransport来实现它. 现在GET请求似乎工作正常.但是,当我从IE9
我一直在疯狂,试图让 jquery.ajax与ie9一起工作.所以我有一个实现CORS的ASP Web API 2 Rest API.所有浏览器的CORS请求都有效. IE9没有用,因为它使用XDomainRequest.我设法通过为IE9自定义实现ajaxTransport来实现它.

现在GET请求似乎工作正常.但是,当我从IE9发出一个帖子请求时,我收到一个HTTP错误415 – 未报告的媒体类型.

我已经将内容类型设置为:“application / json”,我也尝试过“application / x-www-form-urlencoded”,但是根据我的理解XDomainRequest并不支持自定义标题的所有内容?有人知道是否需要在WebAPI上设置特定内容,还是需要调整请求?

我的请求看起来像这样:

$.ajax({
                        url: hostname + "/api/DDC/Book",type: "POST",contentType: "application/json",data: {
                            DealID: function () {
                                return viewModel.get("DealID");
                            },LocationID: function () {
                                return viewModel.get("LocationID");
                            },Time: function () {
                                return viewModel.get("selectedDateTime.Time");
                            }

                        }
                    })

在服务器上我有这个:

[HttpPost("DDC/Book")]
    [EnableCors(origins: "*",headers: "*",methods: "POST,GET,OPTIONS,PUT,DELETE")]
    public dynamic Post(BookModel model)
    {
       .........

当我在IE调试器中分析失败的请求时,这是发送出去的请求标头:

Key Value
Request POST //api/DDC/Book HTTP/1.1
Accept  */*
Origin  http://myurl.com
Accept-Language hr-HR
Accept-Encoding gzip,deflate
User-Agent  Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Host    www.somehost.com
Content-Length  55
DNT 1
Connection  Keep-Alive
Cache-Control   no-cache

我真的失去了所有的希望,IE让我发疯了(该死的是你的微软:D),所以任何帮助或建议都非常适合.

编辑:从更多的研究,我发现WebAPI要求内容类型工作,XDomainRequest不发送一个.因此,我看到的唯一解决方案是调整我的webapi,以便在没有设置时使用默认内容类型.虽然不知道如何做到这一点

EDIT2:通过将我的所有POST转换为GET来暂时破解我的方式,不知道这有多聪明,但我现在看到它没有更大的问题,所以它会一直存在直到我解决问题

解决方法

管理自己解决它.正如Ray Nicholus指出的那样,没有Content-Type ASP Web API默认为“application / octet-stream”Content-Type.我需要默认的“application / x-www-form-urlencoded”.

我设法通过编写自己的简单消息处理程序来检查传入的请求“Content-Type”,如果没有任何内容,则添加“application / x-www-form-urlencoded”.

这是代码:

public class DefaultContentTypeMessageHandler : DelegatingHandler
{
    protected async override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,CancellationToken cancellationToken)
    {
        if (request.Content.Headers.ContentType == null)
            request.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/x-www-form-urlencoded");


        var response = await base.SendAsync(request,cancellationToken);


        return response;
    }

}

更新:

正如罗伯特·基督在下面的评论中所写,我正在为那些之前没有使用过消息处理程序的人扩展答案:

For those who don’t understand at first glance,DelegatingHandlers
allow you to modify requests / response objects before they really hit
the WebAPI framework internals. Nothing else in the framework really
lets you modify the incoming request before model binding,without
actually writing custom model binders (eugh). so instead,here,you
can sniff out a null content type (which is guaranteed by shortcomings
in the XDomainRequest spec),update it to xml or json,and you will be
able to parse the incoming request correctly.

编写消息处理程序后,需要使用WebAPI进行注册.您可以在WebApiConfig类中执行此操作:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {

        config.MessageHandlers.Add(new DefaultContentTypeMessageHandler());
        // Rest of your code

     }
 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读