本篇內(nèi)容介紹了“.Net基于MVC4 Web Api輸出Json格式的方法”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
本文實(shí)例講述了.Net基于MVC4 Web Api輸出Json格式的方法,分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
1、Global 中增加json輸出
復(fù)制代碼 代碼如下:
GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("json", "true", "application/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解析
復(fù)制代碼 代碼如下:
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
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對(duì)象
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
}
3、指定返回格式
新建方法 需要程序集:
復(fù)制代碼 代碼如下:
System.Web.Extensions
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對(duì)象輸出
復(fù)制代碼 代碼如下:
public HttpResponseMessage GetString(string name)
{
return ToJson(name);
}
4、重寫默認(rèn)實(shí)現(xiàn)類 所有輸出將被重新解析成 json
新建JsonContentNegotiator 類
復(fù)制代碼 代碼如下:
public class JsonContentNegotiator : IContentNegotiator
{
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中使用重寫
復(fù)制代碼 代碼如下:
public static void Register(HttpConfiguration config)
{
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));
// 取消注釋下面的代碼行可對(duì)具有 IQueryable 或 IQueryable<T> 返回類型的操作啟用查詢支持。
// 若要避免處理意外查詢或惡意查詢,請(qǐng)使用 QueryableAttribute 上的驗(yàn)證設(shè)置來驗(yàn)證傳入查詢。
// 有關(guān)詳細(xì)信息,請(qǐng)?jiān)L問 /tupian/20230522/ref=go&linkid=0 //config.EnableQuerySupport();
// 若要在應(yīng)用程序中禁用跟蹤,請(qǐng)注釋掉或刪除以下代碼行
// 有關(guān)詳細(xì)信息,請(qǐng)參閱: http://www.asp.net/web-api
config.EnableSystemDiagnosticsTracing();
}
“.Net基于MVC4 Web Api輸出Json格式的方法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
當(dāng)前文章:.Net基于MVC4WebApi輸出Json格式的方法-創(chuàng)新互聯(lián)
文章路徑:http://jinyejixie.com/article34/coeipe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、手機(jī)網(wǎng)站建設(shè)、用戶體驗(yàn)、云服務(wù)器、微信公眾號(hào)、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容