RouteAttribute.cs 6.5 KB

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