AddressController.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using trieTree.xiaoshi.sz.com;
  4. namespace AddressPaser.Controllers
  5. {
  6. [Route("api/[controller]/[action]")]
  7. [ApiController]
  8. public class AddressController : ControllerBase
  9. {
  10. private readonly AddressUtility parser;
  11. public AddressController(AddressUtility mySingleton)
  12. {
  13. parser = mySingleton;
  14. }
  15. [HttpGet(Name = "Paser")]
  16. public ResponseResult<Address> Paser(string Address)
  17. {
  18. try
  19. {
  20. List<string>? strings = parser.Paser(Address);
  21. if (strings != null)
  22. {
  23. ResponseResult<Address> ret = new ResponseResult<Address>();
  24. ret.Status = 200;
  25. ret.Message = string.Empty;
  26. ret.Result = new Address();
  27. ret.Result.OriginalAddress = Address;
  28. if (strings.Count > 0)
  29. {
  30. ret.Result.Province = strings[0];
  31. }
  32. if (strings.Count > 1)
  33. {
  34. ret.Result.City = strings[1];
  35. }
  36. if (strings.Count > 2)
  37. {
  38. ret.Result.Area = strings[2];
  39. }
  40. return ret;
  41. }
  42. else
  43. {
  44. ResponseResult<Address> ret = new ResponseResult<Address>();
  45. ret.Status = 500;
  46. ret.Message = "解析地址没有得到结果,请确认给出的地址是否正确!";
  47. ret.Result = new Address();
  48. ret.Result.OriginalAddress = Address;
  49. return ret;
  50. }
  51. }
  52. catch (Exception ex)
  53. {
  54. ResponseResult<Address> ret = new ResponseResult<Address>();
  55. ret.Status = 500;
  56. ret.Message = $"解析地址出错[{ex.Message}]";
  57. ret.Result = new Address();
  58. ret.Result.OriginalAddress = Address;
  59. return ret;
  60. }
  61. }
  62. }
  63. }