• WebApi返回Json


    参考地址:http://www.cnblogs.com/acles/archive/2013/06/21/3147667.html

     1  public static void Register(HttpConfiguration config)
     2         {
     3             config.Routes.MapHttpRoute(
     4                 name: "ApiV1",
     5                 routeTemplate: "datasvc/v1/{action}",
     6                 defaults: new { controller = "ApiV1" }
     7             );
     8             config.EnableSystemDiagnosticsTracing();
     9 
    10             var jsonFormatter = new JsonMediaTypeFormatter();
    11             config.Services.Replace(typeof(IContentNegotiator), new 
    12               JsonContentNegotiator(jsonFormatter));
    13         }

    ----------------------------------------------------------------------------------------

    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;
            }
        }
  • 相关阅读:
    java中 == 与equals 的区别
    java中的多线程 // 基础
    MySQL-锁机制
    将博客搬至CSDN
    MySQL-事务
    MySQL-存储过程
    MySQL-触发器
    MySQL-视图
    Redis设置Auth认证保护
    PHP目前常见的五大运行模式
  • 原文地址:https://www.cnblogs.com/sunzhenyong/p/9928682.html
Copyright © 2020-2023  润新知