Asp.Net MVC 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";
}