Startup.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using Newtonsoft.Json;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using EVCB_OCPP.DBAPI.Middleware;
  4. using System.Reflection.PortableExecutable;
  5. namespace EVCB_OCPP.DBAPI;
  6. public class Startup
  7. {
  8. public Startup(IConfiguration configuration)
  9. {
  10. Configuration = configuration;
  11. }
  12. public IConfiguration Configuration { get; }
  13. public void ConfigureServices(IServiceCollection services)
  14. {
  15. JsonConvert.DefaultSettings = () => new JsonSerializerSettings()
  16. {
  17. NullValueHandling = NullValueHandling.Ignore,
  18. DateFormatString = "yyyy-MM-dd'T'HH':'mm':'ss'Z'"
  19. };
  20. services.AddControllersWithViews()
  21. .AddControllersAsServices()
  22. // Newtonsoft.Json is added for compatibility reasons
  23. // The recommended approach is to use System.Text.Json for serialization
  24. // Visit the following link for more guidance about moving away from Newtonsoft.Json to System.Text.Json
  25. // https://docs.microsoft.com/dotnet/standard/serialization/system-text-json-migrate-from-newtonsoft-how-to
  26. .AddNewtonsoftJson(options =>
  27. {
  28. options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
  29. options.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.None;
  30. options.SerializerSettings.DateFormatString = "yyyy/MM/dd'T'HH':'mm':'ss'Z'";
  31. options.UseMemberCasing();
  32. });
  33. }
  34. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  35. {
  36. if (env.IsDevelopment())
  37. {
  38. app.UseDeveloperExceptionPage();
  39. }
  40. else
  41. {
  42. app.UseExceptionHandler("/Home/Error");
  43. }
  44. app.UseRouting();
  45. app.EnableRequestBodyRewind();
  46. app.UseMiddleware<APILogMiddleware>();
  47. app.UseMiddleware<ExceptionMiddleware>();
  48. app.UseEndpoints(endpoints =>
  49. {
  50. endpoints.MapControllerRoute(
  51. name: "default",
  52. pattern: "{controller=Home}/{action=Index}/{id?}");
  53. });
  54. }
  55. }