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

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

.Net基于MVC4 Web Api輸出Json格式實例

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

本文實例講述了.Net基于MVC4 Web Api輸出Json格式的方法,分享給大家供大家參考。具體實現方法如下:

1、Global 中增加json輸出

復制代碼 代碼如下:
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解析

復制代碼 代碼如下:
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的解析 當返回值是string 時 直接返回string不是json對象
    GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
}

3、指定返回格式

新建方法 需要程序集:

復制代碼 代碼如下:
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;
}

 調用戶方法轉換為json對象輸出

復制代碼 代碼如下:
public HttpResponseMessage GetString(string name)
{
     return ToJson(name);
}

4、重寫默認實現類 所有輸出將被重新解析成 json

新建JsonContentNegotiator 類

復制代碼 代碼如下:
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中使用重寫

復制代碼 代碼如下:
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 上的驗證設置來驗證傳入查詢。
    // 有關詳細信息,請訪問 http://go.microsoft.com/fwlink/?LinkId=279712。
    //config.EnableQuerySupport();

    // 若要在應用程序中禁用跟蹤,請注釋掉或刪除以下代碼行
    // 有關詳細信息,請參閱: http://www.asp.net/web-api
    config.EnableSystemDiagnosticsTracing();
}

希望本文所述對大家的asp.net程序設計有所幫助。

您可能感興趣的文章:
  • js遍歷json的key和value的實例
  • 微信小程序通過api接口將json數據展現到小程序示例
  • Bootstrap 填充Json數據的實例代碼
  • 簡單談談MySQL5.7 JSON格式檢索
  • JSON在ASP.NET中使用方法
  • ASP.NET中實現把Json數據轉換為ADO.NET DataSet對象
  • Asp.net配合easyui實現返回json數據實例
  • ASP.NET中MVC使用AJAX調用JsonResult方法并返回自定義錯誤信息
  • 淺談C#.NET、JavaScript和JSON
  • js實現將json數組顯示前臺table中

標簽:張家界 新疆 德陽 白山 江蘇 天門 陽泉 蘭州

巨人網絡通訊聲明:本文標題《.Net基于MVC4 Web Api輸出Json格式實例》,本文關鍵詞  .Net,基于,MVC4,Web,Api,輸出,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《.Net基于MVC4 Web Api輸出Json格式實例》相關的同類信息!
  • 本頁收集關于.Net基于MVC4 Web Api輸出Json格式實例的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 绥德县| 礼泉县| 米泉市| 禄丰县| 芦溪县| 盖州市| 临漳县| 屏东县| 临夏县| 永修县| 福鼎市| 和硕县| 玛沁县| 辽阳市| 嘉荫县| 南投市| 乐平市| 武冈市| 枣强县| 图片| 长兴县| 汶川县| 潮州市| 谢通门县| 金湖县| 绿春县| 安陆市| 德令哈市| 天峨县| 体育| 盖州市| 美姑县| 广南县| 班戈县| 景东| 安图县| 台中市| 河北区| 桃园市| 西昌市| 塔河县|