問:請考慮使用 DataContractResolver(如果你正在使用 DataContractSerializer),或將任何未知類型以靜態方式添加到已知類型的列表
解決:
DataContract和DataMember
using System.Runtime.Serialization; namespace Test { [DataContract] public class LoginRequest { [DataMember(Name = "username")] public string UserName { get; set; } [DataMember(Name = "password")] public string Password { get; set; } } }
JsonProperty
using Newtonsoft.Json; namespace Test { public class LoginRequest { [JsonProperty("username")] public string UserName { get; set; } [JsonProperty("password")] public string Password { get; set; } } }
問:[DataContract] [DataMember] 缺少程序集引用
解決:項目-》右鍵-》添加引用-》找到System.Runtime.Serialization 添加之
問:webapi “ObjectContent`1”類型未能序列化內容類型“application/xml; charset=utf-8”的響應正文。
解決:GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
問:跨域問題解決方案
Access to XMLHttpRequest at 'http://localhost:3232/api/User/GetData' from origin 'http://localhost:3333' has been blocked by CORS policy: The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed.
解決:
解決方案:
1、如果跨域請求發生在相同一級域名不同二級域名之間
例如:a.baidu.com 和 b.baidu.com
跨域直接在邀請求的接口頁面中強制設置域為一級域 document.domain = "baidu.com";
2、設置接口允許ajax跨域訪問
在服務器aspx頁面頭文件里加:
<meta http-equiv="Access-Control-Allow-Origin" content="*" />
在web.config文件中的 system.webServer 節點下 增加如下配置
<system.webServer> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Methods" value="OPTIONS,POST,GET"/> <add name="Access-Control-Allow-Headers" value="x-requested-with"/> <add name="Access-Control-Allow-Origin" value="*" /> </customHeaders> </httpProtocol> </system.webServer>
在jquery發起ajax之前,加入代碼 jQuery.support.cors = true;
例如
問:無法讀取配置節“entityFramework”,因為它缺少節聲明(如果遇到無法讀取配置節“unity”,因為它缺少節聲明,解決辦法一樣)
解決
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
</configSections>
問:類型的異常在 Microsoft.Practices.ServiceLocation.dll 中發生
解決:在Global.asax里面
protected void Application_Start() { GlobalConfiguration.Configure(WebApiConfig.Register); var container = new UnityContainer(); UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity"); section.Containers.Default.Configure(container); ServiceLocator.SetLocatorProvider(() => new UnityServiceLocator(container)); }
點擊windows鍵+R鍵
然后輸入cmd
然后再跳出來的窗口里輸入
netsh firewall show portopening
可以看到打開的所有端口
問:
解決:<meta name="referrer" content="no-referrer" />