zoukankan      html  css  js  c++  java
  • 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;
            }
        }
  • 相关阅读:
    第一篇博文
    重拾javascript系列-JS声明详解之var
    重拾Javascript系列
    AtCoder DP Contest 26题
    DP题
    一众数论
    字符编码
    C# DateTime类型和sqlserver DateTime精度不同
    vs2015中的数据库架构对比工具(New Schema Comparison)
    SqlServer常用语句
  • 原文地址:https://www.cnblogs.com/sunzhenyong/p/9928682.html
Copyright © 2011-2022 走看看