• 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

  • 相关阅读:
    Flask路由系统
    Flask配置方式
    Flask应用启动流程
    Flask简介及使用
    python调用支付宝支付接口
    python调用腾讯云短信接口
    Celery简介以及Django中使用celery
    django中使用redis
    Redis之缓存雪崩、缓存穿透、缓存预热、缓存更新、缓存降级
    git操作
  • 原文地址:https://www.cnblogs.com/hualiu0/p/5003303.html
Copyright © 2020-2023  润新知