2014-10-15 17 views
24

Đây là phương pháp Configure của tôi từ lớp Startup của tôi.Làm thế nào để thêm CamelCasePropertyNamesContractResolver trong Startup.cs?

public void Configure(IApplicationBuilder app) 
{ 
    // Setup configuration sources 
    var configuration = new Configuration(); 
    configuration.AddJsonFile("config.json"); 
    configuration.AddEnvironmentVariables(); 

    // Set up application services 
    app.UseServices(services => 
    { 
     // Add EF services to the services container 
     services.AddEntityFramework() 
      .AddSqlServer(); 

     // Configure DbContext 
     services.SetupOptions<DbContextOptions>(options => 
     { 
      options.UseSqlServer(configuration.Get("Data:DefaultConnection:ConnectionString")); 
     }); 

     // Add Identity services to the services container 
     services.AddIdentitySqlServer<ApplicationDbContext, ApplicationUser>() 
      .AddAuthentication(); 

     // Add MVC services to the services container 
     services.AddMvc(); 
    }); 

    // Enable Browser Link support 
    app.UseBrowserLink(); 

    // Add static files to the request pipeline 
    app.UseStaticFiles(); 

    // Add cookie-based authentication to the request pipeline 
    app.UseCookieAuthentication(new CookieAuthenticationOptions 
    { 
     AuthenticationType = ClaimsIdentityOptions.DefaultAuthenticationType, 
     LoginPath = new PathString("/Account/Login"), 
    }); 

    // Add MVC to the request pipeline 
    app.UseMvc(routes => 
    { 
     routes.MapRoute(
      name: "default", 
      template: "{controller}/{action}/{id?}", 
      defaults: new { controller = "Home", action = "Index" }); 

     routes.MapRoute(
      name: "api", 
      template: "{controller}/{id?}"); 
    }); 
} 

Tôi có thể truy cập Trong trường hợp ví dụ HttpConfiguration, để tôi có thể thiết lập các CamelCasePropertyNamesContractResolver, giống như tôi đã làm trong WebAPI 2:

var formatterSettings = config.Formatters.JsonFormatter.SerializerSettings; 
formatterSettings.Formatting = Formatting.None; 
formatterSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); 

Trả lời

23

Chức năng Cấu hình đã bị xóa khỏi services.AddMvc() trong cả hai Beta 6 hoặc 7. Đối với Beta 7, trong Startup.cs, thêm dòng sau vào các ConfigureServices chức năng:

services.AddMvc().AddJsonOptions(options => 
{ 
    options.SerializerSettings.ContractResolver = 
     new CamelCasePropertyNamesContractResolver(); 
}); 
+5

Bạn cũng cần thêm tham chiếu đang sử dụng vào 'Newtonsoft.Json.Serialization'. – Pharylon

40

Thay services.AddMvc(); với những điều sau đây.

services.AddMvc().SetupOptions<MvcOptions>(options => 
{ 
    int position = options.OutputFormatters.FindIndex(f => 
            f.Instance is JsonOutputFormatter); 

    var settings = new JsonSerializerSettings() 
    { 
     ContractResolver = new CamelCasePropertyNamesContractResolver() 
    }; 
    var formatter = new JsonOutputFormatter(settings, false); 

    options.OutputFormatters.Insert(position, formatter); 
}); 

CẬP NHẬT

Với phiên bản hiện tại của ASP.NET, điều này sẽ làm.

services.AddMvc().Configure<MvcOptions>(options => 
{ 
    options.OutputFormatters 
       .Where(f => f.Instance is JsonOutputFormatter) 
       .Select(f => f.Instance as JsonOutputFormatter) 
       .First() 
       .SerializerSettings 
       .ContractResolver = new CamelCasePropertyNamesContractResolver(); 
}); 

UPDATE 2

Với ASP.NET 5 beta5, được vận chuyển với Visual Studio 2015 RTM, đoạn code sau làm việc

services.AddMvc().Configure<MvcOptions>(options => 
{ 
    options.OutputFormatters.OfType<JsonOutputFormatter>() 
      .First() 
      .SerializerSettings 
      .ContractResolver = new CamelCasePropertyNamesContractResolver(); 
}); 

CẬP NHẬT 3

Với ASP.NET 5 beta7, mã sau hoạt động

services.AddMvc().AddJsonOptions(opt => 
{ 
    opt.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); 
}); 

Cập nhật RC2

MVC hiện đang nối tiếp JSON với tên trường hợp camel theo mặc định. Xem thông báo này. https://github.com/aspnet/Announcements/issues/194

+0

Tại sao trong ASP .NET 5 bạn đã chèn một trình định dạng mới và không chỉ thay đổi trình định dạng hiện tại? – alisabzevari

+0

Vì vậy, bạn tìm thông tin này ở đâu? Tôi không thể tìm thấy nó trong các tài liệu trên asp.net. Có một vị trí API nó có thể được tìm kiếm? – Jon49

Các vấn đề liên quan