owin – 如何在Startup.cs中添加CamelCasePropertyNamesContractResolver?

前端之家收集整理的这篇文章主要介绍了owin – 如何在Startup.cs中添加CamelCasePropertyNamesContractResolver?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的启动类中的配置方法
  1. public void Configure(IApplicationBuilder app)
  2. {
  3. // Setup configuration sources
  4. var configuration = new Configuration();
  5. configuration.AddJsonFile("config.json");
  6. configuration.AddEnvironmentVariables();
  7.  
  8. // Set up application services
  9. app.UseServices(services =>
  10. {
  11. // Add EF services to the services container
  12. services.AddEntityFramework()
  13. .AddsqlServer();
  14.  
  15. // Configure DbContext
  16. services.SetupOptions<DbContextOptions>(options =>
  17. {
  18. options.UsesqlServer(configuration.Get("Data:DefaultConnection:ConnectionString"));
  19. });
  20.  
  21. // Add Identity services to the services container
  22. services.AddIdentitysqlServer<ApplicationDbContext,ApplicationUser>()
  23. .AddAuthentication();
  24.  
  25. // Add MVC services to the services container
  26. services.AddMvc();
  27. });
  28.  
  29. // Enable Browser Link support
  30. app.UseBrowserLink();
  31.  
  32. // Add static files to the request pipeline
  33. app.UseStaticFiles();
  34.  
  35. // Add cookie-based authentication to the request pipeline
  36. app.UseCookieAuthentication(new CookieAuthenticationOptions
  37. {
  38. AuthenticationType = ClaimsIdentityOptions.DefaultAuthenticationType,LoginPath = new PathString("/Account/Login"),});
  39.  
  40. // Add MVC to the request pipeline
  41. app.UseMvc(routes =>
  42. {
  43. routes.MapRoute(
  44. name: "default",template: "{controller}/{action}/{id?}",defaults: new { controller = "Home",action = "Index" });
  45.  
  46. routes.MapRoute(
  47. name: "api",template: "{controller}/{id?}");
  48. });
  49. }

在哪里可以访问HttpConfiguration实例,以便我可以设置CamelCasePropertyNamesContractResolver,就像我在WebApi 2中所做的那样:

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

解决方法

配置功能已从Beta 6或7中的services.AddMvc()中删除。对于Beta 7,在Startup.cs中,将以下内容添加到ConfigureServices函数中:
  1. services.AddMvc().AddJsonOptions(options =>
  2. {
  3. options.SerializerSettings.ContractResolver =
  4. new CamelCasePropertyNamesContractResolver();
  5. });

猜你在找的asp.Net相关文章