MySelector.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using Microsoft.AspNetCore.Mvc.RazorPages;
  2. using Microsoft.AspNetCore.Mvc.RazorPages.Infrastructure;
  3. using Microsoft.Extensions.Primitives;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. namespace SupplierWeb.Codes
  9. {
  10. public class MySelector : IPageHandlerMethodSelector
  11. {
  12. private const string Handler = "handler";
  13. public HandlerMethodDescriptor Select(PageContext context)
  14. {
  15. var handlers = SelectHandlers(context);
  16. if (handlers == null || handlers.Count == 0)
  17. {
  18. return null;
  19. }
  20. List<HandlerMethodDescriptor> ambiguousMatches = null;
  21. HandlerMethodDescriptor bestMatch = null;
  22. for (var score = 2; score >= 0; score--)
  23. {
  24. for (var i = 0; i < handlers.Count; i++)
  25. {
  26. var handler = handlers[i];
  27. if (GetScore(handler) == score)
  28. {
  29. if (bestMatch == null)
  30. {
  31. bestMatch = handler;
  32. continue;
  33. }
  34. if (ambiguousMatches == null)
  35. {
  36. ambiguousMatches = new List<HandlerMethodDescriptor>();
  37. ambiguousMatches.Add(bestMatch);
  38. }
  39. ambiguousMatches.Add(handler);
  40. }
  41. }
  42. if (ambiguousMatches != null)
  43. {
  44. var ambiguousMethods = string.Join(", ", ambiguousMatches.Select(m => m.MethodInfo));
  45. throw new InvalidOperationException("more handler");
  46. }
  47. if (bestMatch != null)
  48. {
  49. return bestMatch;
  50. }
  51. }
  52. return null;
  53. }
  54. private static List<HandlerMethodDescriptor> SelectHandlers(PageContext context)
  55. {
  56. var handlers = context.ActionDescriptor.HandlerMethods;
  57. List<HandlerMethodDescriptor> handlersToConsider = null;
  58. var handlerName = Convert.ToString(context.RouteData.Values[Handler]);
  59. if (string.IsNullOrEmpty(handlerName) &&
  60. context.HttpContext.Request.Query.TryGetValue(Handler, out StringValues queryValues))
  61. {
  62. handlerName = queryValues[0];
  63. }
  64. for (var i = 0; i < handlers.Count; i++)
  65. {
  66. var handler = handlers[i];
  67. if (handler.HttpMethod != null &&
  68. !string.Equals(handler.HttpMethod, context.HttpContext.Request.Method, StringComparison.OrdinalIgnoreCase))
  69. {
  70. continue;
  71. }
  72. else if (handler.Name != null &&
  73. !handler.Name.Equals(handlerName, StringComparison.OrdinalIgnoreCase))
  74. {
  75. continue;
  76. }
  77. if (handlersToConsider == null)
  78. {
  79. handlersToConsider = new List<HandlerMethodDescriptor>();
  80. }
  81. handlersToConsider.Add(handler);
  82. }
  83. return handlersToConsider;
  84. }
  85. private static int GetScore(HandlerMethodDescriptor descriptor)
  86. {
  87. if (descriptor.Name != null)
  88. {
  89. return 2;
  90. }
  91. else if (descriptor.HttpMethod != null)
  92. {
  93. return 1;
  94. }
  95. else
  96. {
  97. return 0;
  98. }
  99. }
  100. }
  101. }