zoukankan      html  css  js  c++  java
  • config .net webapi to return json.

    1.add content negotiator

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net.Http;
    using System.Net.Http.Formatting;
    using System.Net.Http.Headers;
    using System.Web;
    
    namespace PtvV2ToolWebApi
    {
        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;
            }
        }
    }
    View Code

    2.add below code in app_start folder webapiconfig.cs to register config

    using Newtonsoft.Json.Serialization;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net.Http.Formatting;
    using System.Web.Http;
    
    namespace PtvV2ToolWebApi
    {
        public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
    
                // Remove the XML formatter
                config.Formatters.Remove(config.Formatters.XmlFormatter);
    
                // Web API configuration and services
                var json = config.Formatters.JsonFormatter;
                //
                json.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
    
                //
                var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
                jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    
                config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter));
    
                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );
    
                // Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type.
                // To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries.
                // For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712.
                //config.EnableQuerySupport();
    
                // To disable tracing in your application, please comment out or remove the following line of code
                // For more information, refer to: http://www.asp.net/web-api
    
    
                config.EnableSystemDiagnosticsTracing();
            }
        }
    }
    View Code

    at last return list auto change to json

  • 相关阅读:
    IOS学习笔记3—Objective C—简单的内存管理
    IOS学习笔记2—Objective C—类、属性、方法
    IOS学习笔记1—Iphone程序运行流程
    IOS学习笔记37——ViewController生命周期详解
    [IOS初学]ios 第一篇 storyboard 与viewcontroller的关系
    iOS: 如何正确的绘制1像素的线
    Swift 2.0初探:值得注意的新特性
    Swift语言Storyboard教程:第二部
    C# partial 关键字详解
    spring-boot工程中,jpa下hibernate的ddl-auto的各种属性
  • 原文地址:https://www.cnblogs.com/hualiu0/p/5003303.html
Copyright © 2011-2022 走看看