WebApiModule.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Reflection;
  3. using System.Web.Http;
  4. using Abp.Application.Services;
  5. using Abp.Configuration.Startup;
  6. using Abp.Json;
  7. using Abp.Modules;
  8. using Abp.Timing;
  9. using Abp.WebApi;
  10. using Abp.WebApi.Authorization;
  11. using Abp.WebApi.Configuration;
  12. using Newtonsoft.Json.Serialization;
  13. namespace ShwasherSys
  14. {
  15. [DependsOn(typeof(AbpWebApiModule), typeof(ShwasherApplicationModule))]
  16. public class IwbYueWebApiModule : AbpModule
  17. {
  18. public override void PreInitialize()
  19. {
  20. //配置所有Cache的默认过期时间为2小时
  21. Configuration.Caching.ConfigureAll(cache =>
  22. {
  23. cache.DefaultSlidingExpireTime = TimeSpan.FromHours(2);
  24. });
  25. }
  26. public override void Initialize()
  27. {
  28. IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
  29. Configuration.Modules.AbpWebApi().DynamicApiControllerBuilder
  30. .ForAll<IApplicationService>(typeof(ShwasherApplicationModule).Assembly, "app")
  31. .Build();
  32. }
  33. public override void PostInitialize()
  34. {
  35. GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new IwbCamelCasePropertyNamesContractResolver();
  36. var httpConfiguration = IocManager.Resolve<IAbpWebApiConfiguration>().HttpConfiguration;
  37. //httpConfiguration.Services.Replace(typeof(AbpApiAuthorizeFilter), IocManager.Resolve<IwbApiAuthorizeFilter>());
  38. httpConfiguration.Filters.Remove(IocManager.Resolve<AbpApiAuthorizeFilter>());
  39. httpConfiguration.Filters.Add(IocManager.Resolve<ShwasherApiAuthorizeFilter>());
  40. }
  41. }
  42. public class IwbCamelCasePropertyNamesContractResolver : AbpCamelCasePropertyNamesContractResolver
  43. {
  44. protected override void ModifyProperty(MemberInfo member, JsonProperty property)
  45. {
  46. if (property.PropertyType != typeof(DateTime) && property.PropertyType != typeof(DateTime?))
  47. {
  48. return;
  49. }
  50. if (member.GetMemberSingleAttribute<DisableDateTimeNormalizationAttribute>() == null)
  51. {
  52. property.Converter = new AbpDateTimeConverter() { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
  53. }
  54. }
  55. }
  56. }