webapi输出炜json_WebApi返回Json格式
namespaceTestWebApi
{public static classWebApiConfig
{public static voidRegister(HttpConfiguration config)
{//Web API 配置和服務
#region [=>1、WebApi 返回JSON,不推薦做法性能不高]
/*config.Formatters.Clear();
config.Formatters.Remove(config.Formatters.XmlFormatter);
config.Formatters.Add(new JsonMediaTypeFormatter());
config.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings
{
ContractResolver = new UnderlineSplitContractResolver(), //小寫命名法。
DateFormatString = "yyyy-MM-dd HH:mm:ss",//解決json時間帶T的問題
Formatting = Newtonsoft.Json.Formatting.Indented,//解決json格式化縮進問題
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore//解決json序列化時的循環(huán)引用問題
};*/
#endregion
#region [=>2、WebApi 返回JSON,推薦做法性能最高]config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(newJsonMediaTypeFormatter()));#endregion
//Web API 路由
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name:"DefaultApi",
routeTemplate:"api/{controller}/{action}/{id}",
defaults:new { id =RouteParameter.Optional }
);
}
}///
///在全局設置中,使用自定義的只返回Json Result。只讓api接口中替換xml,返回json。這種方法的性能是最高的!///
public classJsonContentNegotiator : IContentNegotiator
{private readonlyJsonMediaTypeFormatter _jsonFormatter;publicJsonContentNegotiator(JsonMediaTypeFormatter formatter)
{
_jsonFormatter=formatter;
}public ContentNegotiationResult Negotiate(Type type, HttpRequestMessage request, IEnumerableformatters)
{//對 JSON 數(shù)據(jù)使用混合大小寫。駝峰式,但是是javascript 首字母小寫形式.小駝峰命名法。//config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();//對 JSON 數(shù)據(jù)使用混合大小寫。跟屬性名同樣的大小.輸出
_jsonFormatter.SerializerSettings.ContractResolver = new UnderlineSplitContractResolver(); //小寫命名法。
_jsonFormatter.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";//解決json時間帶T的問題
_jsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;//解決json格式化縮進問題
_jsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;//解決json序列化時的循環(huán)引用問題
var result = new ContentNegotiationResult(_jsonFormatter, new MediaTypeHeaderValue("application/json"));returnresult;
}
}///
///Json.NET 利用ContractResolver解決命名不一致問題
///解決問題:通過無論是序列化還是反序列化都達到了效果,即:ProjectName -> project_name 和 project_name -> ProjectName///
public classUnderlineSplitContractResolver : DefaultContractResolver
{protected override string ResolvePropertyName(stringpropertyName)
{return CamelCaseToUnderlineSplit(propertyName);//下劃線分割命名法//return propertyName.ToLower();//小寫命名法
}private string CamelCaseToUnderlineSplit(stringname)
{
StringBuilder builder= newStringBuilder();for (int i = 0; i < name.Length; i++)
{var ch =name[i];if (char.IsUpper(ch) && i > 0)
{var prev = name[i - 1];if (prev != '_')
{if (char.IsUpper(prev))
{if (i < name.Length - 1)
{var next = name[i + 1];if (char.IsLower(next))
{
builder.Append('_');
}
}
}else{
builder.Append('_');
}
}
}
builder.Append(char.ToLower(ch));
}returnbuilder.ToString();
}
}
}
總結
以上是生活随笔為你收集整理的webapi输出炜json_WebApi返回Json格式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: markdown python整段话_(
- 下一篇: linux mate桌面主题下载_7款L