.Net基于MVC4 Web Api輸出Json格式實(shí)例
本文實(shí)例講述了.Net基于MVC4 Web Api輸出Json格式的方法,分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
1、Global 中增加json輸出
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
//添加json 解析 使用方法 http://xxx/api/action?json=true
GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("json", "true", "application/json"));
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
2、Global 中刪除xml解析
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
//刪除xml的解析 當(dāng)返回值是string 時(shí) 直接返回string不是json對象
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
}
3、指定返回格式
新建方法 需要程序集:
public static HttpResponseMessage ToJson(Object obj)
{
String str;
if (obj is String || obj is Char)
{
str = obj.ToString();
}
else
{
var serializer = new JavaScriptSerializer();
str = serializer.Serialize(obj);
}
var result = new HttpResponseMessage { Content = new StringContent(str, Encoding.GetEncoding("UTF-8"), "application/json") };
return result;
}
調(diào)用戶方法轉(zhuǎn)換為json對象輸出
{
return ToJson(name);
}
4、重寫默認(rèn)實(shí)現(xiàn)類 所有輸出將被重新解析成 json
新建JsonContentNegotiator 類
{
private readonly JsonMediaTypeFormatter _jsonFormatter;
public JsonContentNegotiator(JsonMediaTypeFormatter formatter)
{
_jsonFormatter = formatter;
}
public ContentNegotiationResult Negotiate(Type type, HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters)
{
var result = new ContentNegotiationResult(_jsonFormatter, new MediaTypeHeaderValue("application/json"));
return result;
}
}
WebApiConfig中使用重寫
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var jsonFormatter = new JsonMediaTypeFormatter();
config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter));
// 取消注釋下面的代碼行可對具有 IQueryable 或 IQueryable<T> 返回類型的操作啟用查詢支持。
// 若要避免處理意外查詢或惡意查詢,請使用 QueryableAttribute 上的驗(yàn)證設(shè)置來驗(yàn)證傳入查詢。
// 有關(guān)詳細(xì)信息,請?jiān)L問 http://go.microsoft.com/fwlink/?LinkId=279712。
//config.EnableQuerySupport();
// 若要在應(yīng)用程序中禁用跟蹤,請注釋掉或刪除以下代碼行
// 有關(guān)詳細(xì)信息,請參閱: http://www.asp.net/web-api
config.EnableSystemDiagnosticsTracing();
}
希望本文所述對大家的asp.net程序設(shè)計(jì)有所幫助。
- js遍歷json的key和value的實(shí)例
- 微信小程序通過api接口將json數(shù)據(jù)展現(xiàn)到小程序示例
- Bootstrap 填充Json數(shù)據(jù)的實(shí)例代碼
- 簡單談?wù)凪ySQL5.7 JSON格式檢索
- JSON在ASP.NET中使用方法
- ASP.NET中實(shí)現(xiàn)把Json數(shù)據(jù)轉(zhuǎn)換為ADO.NET DataSet對象
- Asp.net配合easyui實(shí)現(xiàn)返回json數(shù)據(jù)實(shí)例
- ASP.NET中MVC使用AJAX調(diào)用JsonResult方法并返回自定義錯(cuò)誤信息
- 淺談C#.NET、JavaScript和JSON
- js實(shí)現(xiàn)將json數(shù)組顯示前臺(tái)table中
相關(guān)文章
datagrid和repeader控件中替換標(biāo)識值的方法
本節(jié)主要介紹了datagrid和repeader控件中替換標(biāo)識值的方法,需要的朋友可以參考下2014-08-08
asp.net AJAX實(shí)現(xiàn)無刷新獲得數(shù)據(jù)
提供一個(gè)使用AJAX實(shí)現(xiàn)無刷新判斷注冊用戶名是否被注冊的代碼:2008-11-11
WPF在自定義文本框中實(shí)現(xiàn)輸入法跟隨光標(biāo)
本文主要為大家介紹了如何在WPF寫一個(gè)自定義的文本框,并且能實(shí)現(xiàn)讓輸入法跟隨光標(biāo)。文中的示例代碼講解詳細(xì),需要的可以參考一下2022-02-02
Discuz!NT 3與asp.net 整合的實(shí)例教程
本次整合只針對NETSNS中的代碼做了少許修改,完成了基本的和論壇同步注冊,登陸和注銷,信息獲取,信息修改。為的是給各位Discuz!NT API愛好者做一個(gè)簡單的API事例,供大家參考。2009-11-11

