.Net基于MVC4 Web Api輸出Json格式實例
本文實例講述了.Net基于MVC4 Web Api輸出Json格式的方法,分享給大家供大家參考。具體實現(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 時 直接返回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)實現(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 上的驗證設(shè)置來驗證傳入查詢。
// 有關(guān)詳細(xì)信息,請訪問 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è)計有所幫助。
- js遍歷json的key和value的實例
- 微信小程序通過api接口將json數(shù)據(jù)展現(xiàn)到小程序示例
- Bootstrap 填充Json數(shù)據(jù)的實例代碼
- 簡單談?wù)凪ySQL5.7 JSON格式檢索
- JSON在ASP.NET中使用方法
- ASP.NET中實現(xiàn)把Json數(shù)據(jù)轉(zhuǎn)換為ADO.NET DataSet對象
- Asp.net配合easyui實現(xiàn)返回json數(shù)據(jù)實例
- ASP.NET中MVC使用AJAX調(diào)用JsonResult方法并返回自定義錯誤信息
- 淺談C#.NET、JavaScript和JSON
- js實現(xiàn)將json數(shù)組顯示前臺table中
相關(guān)文章
datagrid和repeader控件中替換標(biāo)識值的方法
本節(jié)主要介紹了datagrid和repeader控件中替換標(biāo)識值的方法,需要的朋友可以參考下2014-08-08asp.net AJAX實現(xiàn)無刷新獲得數(shù)據(jù)
提供一個使用AJAX實現(xiàn)無刷新判斷注冊用戶名是否被注冊的代碼:2008-11-11WPF在自定義文本框中實現(xiàn)輸入法跟隨光標(biāo)
本文主要為大家介紹了如何在WPF寫一個自定義的文本框,并且能實現(xiàn)讓輸入法跟隨光標(biāo)。文中的示例代碼講解詳細(xì),需要的可以參考一下2022-02-02