RouteAttribute.cs 6.6 KB

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