Asp.Net MVC WebApi 通用问题

本文档提供了针对调用WebService时出现的消息大小限制错误的解决方案,并介绍了如何配置ASP.NET WebAPI以支持更大的消息接收。此外,还解决了跨域访问问题及如何启用HttpContext的Session功能。最后,给出了调整WebAPI日期格式的方法。

1.调用Webservice时报错:

  已超过传入消息(65536)的最大消息大小配额。若要增加配额,请使用相应绑定元素上的 MaxReceivedMessageSize 。

       服务器端加入如下代码没有解决:

    <binding name="LargeDataTransferServicesBinding" sendTimeout="00:10:00"

      maxReceivedMessageSize="2147483647" transferMode="Streamed" messageEncoding="Text" />

      解决方案: 对单个Webservice 添加 maxReceivedMessageSize="2147483647"  实现。

                  <system.serviceModel>
                     <bindings>
                     <basicHttpBinding>
                     <binding name="AAAA" maxReceivedMessageSize="2147483647" />
                     <binding name="BBBB" maxReceivedMessageSize="2147483647" />
                     </basicHttpBinding>
                   </bindings>

 2.跨域问题:

     a.在Global添加代码 

                     //跨域访问1
                     protected void Application_BeginRequest()
                     {
                                   if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS")
                                  {
                                      Response.End();
                                  }
                     }

     b.webconfig 添加代码

 

                 <system.webServer> 中添加
                    <!--跨域访问2-->
                    <httpProtocol>
                    <customHeaders>
                    <add name="Access-Control-Allow-Origin" value="*" />
                    <add name="Access-Control-Allow-Headers" value="Content-Type,key,value" />
                    <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
                    </customHeaders>
                    </httpProtocol>
                  </system.webServer>

 

3.asp.net web api 开启HttpContext 的Session功能

   在Global添加如下代码 

                   public override void Init()
                   {
                      this.PostAuthenticateRequest += (sender, e) => HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
                      base.Init();
                   }

4.WebApi 返回给前端的日期格式带T 

     解决方案: 在Global中Application_Start添加代码 

                 protected void Application_Start()
                 {

                 // 配置返回的时间类型数据格式
                 var format = GlobalConfiguration.Configuration.Formatters;
                 var jsonFormat = format.JsonFormatter;
                 var settings = jsonFormat.SerializerSettings;
                 settings.Formatting = Newtonsoft.Json.Formatting.Indented;
                 settings.DateFormatString = "yyyy-MM-dd HH:mm:ss";

                }

转载于:https://www.cnblogs.com/qiaoxiaoping/p/7611258.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值