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

  • 相关阅读:
    javascript中的光标
    jQuery插件使用大全
    marginCollapse之兄弟关系的DIV
    margin collapse 之父子关系的DIV
    选择符优先级-----:link伪类
    a标签包input引起的问题
    关于inline-block的间隙问题
    创意输入框
    Unity3D教程宝典之Shader篇:第十四讲Surface Shader
    Unity3D教程宝典之Shader篇:第十三讲 Alpha混合
  • 原文地址:https://www.cnblogs.com/hualiu0/p/5003303.html
Copyright © 2011-2022 走看看