RouteAttribute.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. namespace MediaBrowser.Model.Services
  5. {
  6. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
  7. public class RouteAttribute : Attribute
  8. {
  9. /// <summary>
  10. /// <para>Initializes an instance of the <see cref="RouteAttribute"/> class.</para>
  11. /// </summary>
  12. /// <param name="path">
  13. /// <para>The path template to map to the request. See
  14. /// <see cref="Path">RouteAttribute.Path</see>
  15. /// for details on the correct format.</para>
  16. /// </param>
  17. public RouteAttribute(string path)
  18. : this(path, null)
  19. {
  20. }
  21. /// <summary>
  22. /// <para>Initializes an instance of the <see cref="RouteAttribute"/> class.</para>
  23. /// </summary>
  24. /// <param name="path">
  25. /// <para>The path template to map to the request. See
  26. /// <see cref="Path">RouteAttribute.Path</see>
  27. /// for details on the correct format.</para>
  28. /// </param>
  29. /// <param name="verbs">A comma-delimited list of HTTP verbs supported by the
  30. /// service. If unspecified, all verbs are assumed to be supported.</param>
  31. public RouteAttribute(string path, string verbs)
  32. {
  33. Path = path;
  34. Verbs = verbs;
  35. }
  36. /// <summary>
  37. /// Gets or sets the path template to be mapped to the request.
  38. /// </summary>
  39. /// <value>
  40. /// A <see cref="String"/> value providing the path mapped to
  41. /// the request. Never <see langword="null"/>.
  42. /// </value>
  43. /// <remarks>
  44. /// <para>Some examples of valid paths are:</para>
  45. ///
  46. /// <list>
  47. /// <item>"/Inventory"</item>
  48. /// <item>"/Inventory/{Category}/{ItemId}"</item>
  49. /// <item>"/Inventory/{ItemPath*}"</item>
  50. /// </list>
  51. ///
  52. /// <para>Variables are specified within "{}"
  53. /// brackets. Each variable in the path is mapped to the same-named property
  54. /// on the request DTO. At runtime, ServiceStack will parse the
  55. /// request URL, extract the variable values, instantiate the request DTO,
  56. /// and assign the variable values into the corresponding request properties,
  57. /// prior to passing the request DTO to the service object for processing.</para>
  58. ///
  59. /// <para>It is not necessary to specify all request properties as
  60. /// variables in the path. For unspecified properties, callers may provide
  61. /// values in the query string. For example: the URL
  62. /// "http://services/Inventory?Category=Books&amp;ItemId=12345" causes the same
  63. /// request DTO to be processed as "http://services/Inventory/Books/12345",
  64. /// provided that the paths "/Inventory" (which supports the first URL) and
  65. /// "/Inventory/{Category}/{ItemId}" (which supports the second URL)
  66. /// are both mapped to the request DTO.</para>
  67. ///
  68. /// <para>Please note that while it is possible to specify property values
  69. /// in the query string, it is generally considered to be less RESTful and
  70. /// less desirable than to specify them as variables in the path. Using the
  71. /// query string to specify property values may also interfere with HTTP
  72. /// caching.</para>
  73. ///
  74. /// <para>The final variable in the path may contain a "*" suffix
  75. /// to grab all remaining segments in the path portion of the request URL and assign
  76. /// them to a single property on the request DTO.
  77. /// For example, if the path "/Inventory/{ItemPath*}" is mapped to the request DTO,
  78. /// then the request URL "http://services/Inventory/Books/12345" will result
  79. /// in a request DTO whose ItemPath property contains "Books/12345".
  80. /// You may only specify one such variable in the path, and it must be positioned at
  81. /// the end of the path.</para>
  82. /// </remarks>
  83. public string Path { get; set; }
  84. /// <summary>
  85. /// Gets or sets short summary of what the route does.
  86. /// </summary>
  87. public string Summary { get; set; }
  88. public string Description { get; set; }
  89. public bool IsHidden { get; set; }
  90. /// <summary>
  91. /// Gets or sets longer text to explain the behaviour of the route.
  92. /// </summary>
  93. public string Notes { get; set; }
  94. /// <summary>
  95. /// Gets or sets a comma-delimited list of HTTP verbs supported by the service, such as
  96. /// "GET,PUT,POST,DELETE".
  97. /// </summary>
  98. /// <value>
  99. /// A <see cref="String"/> providing a comma-delimited list of HTTP verbs supported
  100. /// by the service, <see langword="null"/> or empty if all verbs are supported.
  101. /// </value>
  102. public string Verbs { get; set; }
  103. /// <summary>
  104. /// Used to rank the precedences of route definitions in reverse routing.
  105. /// i.e. Priorities below 0 are auto-generated have less precedence.
  106. /// </summary>
  107. public int Priority { get; set; }
  108. protected bool Equals(RouteAttribute other)
  109. {
  110. return base.Equals(other)
  111. && string.Equals(Path, other.Path)
  112. && string.Equals(Summary, other.Summary)
  113. && string.Equals(Notes, other.Notes)
  114. && string.Equals(Verbs, other.Verbs)
  115. && Priority == other.Priority;
  116. }
  117. public override bool Equals(object obj)
  118. {
  119. if (ReferenceEquals(null, obj))
  120. {
  121. return false;
  122. }
  123. if (ReferenceEquals(this, obj))
  124. {
  125. return true;
  126. }
  127. if (obj.GetType() != this.GetType())
  128. {
  129. return false;
  130. }
  131. return Equals((RouteAttribute)obj);
  132. }
  133. public override int GetHashCode()
  134. {
  135. unchecked
  136. {
  137. var hashCode = base.GetHashCode();
  138. hashCode = (hashCode * 397) ^ (Path != null ? Path.GetHashCode() : 0);
  139. hashCode = (hashCode * 397) ^ (Summary != null ? Summary.GetHashCode() : 0);
  140. hashCode = (hashCode * 397) ^ (Notes != null ? Notes.GetHashCode() : 0);
  141. hashCode = (hashCode * 397) ^ (Verbs != null ? Verbs.GetHashCode() : 0);
  142. hashCode = (hashCode * 397) ^ Priority;
  143. return hashCode;
  144. }
  145. }
  146. }
  147. }