WebApiConfig.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using EVCB_OCPP.WEBAPI.Handlers;
  2. using Newtonsoft.Json;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Web.Http;
  7. using System.Web.Http.ExceptionHandling;
  8. using static EVCB_OCPP.WEBAPI.Handlers.APILogHandler;
  9. namespace EVCB_OCPP.WEBAPI
  10. {
  11. public static class WebApiConfig
  12. {
  13. public static void Register(HttpConfiguration config)
  14. {
  15. config.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings
  16. {
  17. NullValueHandling = NullValueHandling.Ignore,
  18. DateFormatString = EVCBConfiguration.UTC_DATETIMEFORMAT
  19. };
  20. config.MessageHandlers.Add(new ApiLogHandler());
  21. // Web API 設定和服務
  22. config.Services.Replace(typeof(IExceptionHandler), new GlobalExceptionHandler());
  23. // Web API 路由
  24. config.MapHttpAttributeRoutes();
  25. config.Routes.MapHttpRoute(
  26. name: "DefaultApi",
  27. routeTemplate: "api/v1/{controller}/{id}",
  28. defaults: new { id = RouteParameter.Optional }
  29. );
  30. config.Routes.MapHttpRoute(
  31. name: "Version2",
  32. routeTemplate: "api/v2/{controller}/{id}",
  33. defaults: new { id = RouteParameter.Optional }
  34. );
  35. }
  36. }
  37. }