婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > .Net基于MVC4 Web Api輸出Json格式實(shí)例

.Net基于MVC4 Web Api輸出Json格式實(shí)例

熱門標(biāo)簽:浦發(fā)電話機(jī)器人提醒還款 400電話如何申請取消 騰訊地圖標(biāo)注手機(jī) 百度地圖怎樣做地圖標(biāo)注 電銷語音機(jī)器人型號參數(shù) 柳州電銷機(jī)器人公司 昆明語音電銷機(jī)器人價格 太原400電話上門辦理 征途美甲店地圖標(biāo)注

本文實(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 時 直接返回string不是json對象
    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對象輸出

復(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, IEnumerableMediaTypeFormatter> 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));

    // 取消注釋下面的代碼行可對具有 IQueryable 或 IQueryableT> 返回類型的操作啟用查詢支持。
    // 若要避免處理意外查詢或惡意查詢,請使用 QueryableAttribute 上的驗(yàn)證設(shè)置來驗(yàn)證傳入查詢。
    // 有關(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的實(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方法并返回自定義錯誤信息
  • 淺談C#.NET、JavaScript和JSON
  • js實(shí)現(xiàn)將json數(shù)組顯示前臺table中

標(biāo)簽:張家界 新疆 德陽 白山 江蘇 天門 陽泉 蘭州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《.Net基于MVC4 Web Api輸出Json格式實(shí)例》,本文關(guān)鍵詞  .Net,基于,MVC4,Web,Api,輸出,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《.Net基于MVC4 Web Api輸出Json格式實(shí)例》相關(guān)的同類信息!
  • 本頁收集關(guān)于.Net基于MVC4 Web Api輸出Json格式實(shí)例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 樟树市| 刚察县| 云林县| 汉源县| 施甸县| 和田市| 宣化县| 鸡泽县| 额济纳旗| 泗阳县| 霍州市| 静安区| 铁岭县| 贵德县| 芒康县| 苍梧县| 都江堰市| 沛县| 胶南市| 富蕴县| 凌源市| 于田县| 文化| 通江县| 韶关市| 安溪县| 班戈县| 大邑县| 湄潭县| 湖南省| 兴化市| 亚东县| 罗江县| 墨竹工卡县| 攀枝花市| 红桥区| 绥化市| 钟祥市| 潍坊市| 赣榆县| 文安县|