/vodca.core/Vodca.Extensions/Extensions.QueryString.cs

# · C# · 249 lines · 89 code · 16 blank · 144 comment · 0 complexity · a7a782a68c2653876d0229a9fa3370d4 MD5 · raw file

  1. //-----------------------------------------------------------------------------
  2. // <copyright file="Extensions.QueryString.cs" company="genuine">
  3. // Copyright (c) J.Baltikauskas. All rights reserved.
  4. // </copyright>
  5. //-----------------------------------------------------------------------------
  6. // Author: J.Baltikauskas
  7. // Date: 07/30/2008
  8. //-----------------------------------------------------------------------------
  9. namespace Vodca
  10. {
  11. using System;
  12. using System.Web;
  13. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.StyleCop.CSharp.DocumentationRules", "SA1601:PartialElementsMustBeDocumented", Justification = "Extension methods partial class.")]
  14. public static partial class Extensions
  15. {
  16. /// <summary>
  17. /// Query the string utility. Designed to use inside Web DLL's mostly and legacy support for ASP.NET 2.0.
  18. /// </summary>
  19. /// <param name="param">The param.</param>
  20. /// <returns>Query string param as Nullable byte</returns>
  21. /// <example>View code: <br />
  22. /// byte? id = Extensions.QueryStringAsByte("id");
  23. /// </example>
  24. public static byte? QueryStringAsByte(string param)
  25. {
  26. string value = HttpContext.Current.Request.QueryString[param];
  27. return string.IsNullOrEmpty(value) ? null : value.ConvertToByte();
  28. }
  29. /// <summary>
  30. /// Query the string utility. Designed to use for ASP.NET 3.5.
  31. /// </summary>
  32. /// <param name="request">The HTTP values sent by a client during a Web request</param>
  33. /// <param name="param">The param.</param>
  34. /// <returns>Query string param as Nullable byte</returns>
  35. /// <example>View code: <br />
  36. /// byte? id = this.Page.Request.QueryStringAsByte("id");
  37. /// </example>
  38. public static byte? QueryStringAsByte(this HttpRequest request, string param)
  39. {
  40. string value = request.QueryString[param];
  41. return string.IsNullOrEmpty(value) ? null : value.ConvertToByte();
  42. }
  43. /// <summary>
  44. /// Query the string utility. Designed to use inside Web DLL's mostly and legacy support for ASP.NET 2.0.
  45. /// </summary>
  46. /// <param name="param">The param.</param>
  47. /// <returns>Query string param as Nullable int</returns>
  48. /// <example>View code: <br />
  49. /// int? id = Extensions.QueryStringAsInt("id");
  50. /// </example>
  51. public static int? QueryStringAsInt(string param)
  52. {
  53. string value = HttpContext.Current.Request.QueryString[param];
  54. return string.IsNullOrEmpty(value) ? null : value.ConvertToInt();
  55. }
  56. /// <summary>
  57. /// Query the string utility. Designed to use for ASP.NET 3.5.
  58. /// </summary>
  59. /// <param name="request">The HTTP values sent by a client during a Web request</param>
  60. /// <param name="param">The param.</param>
  61. /// <returns>Query string param as Nullable int</returns>
  62. /// <example>View code: <br />
  63. /// int? id = this HttpRequest request, .QueryStringAsInt("id");
  64. /// </example>
  65. public static int? QueryStringAsInt(this HttpRequest request, string param)
  66. {
  67. string value = request.QueryString[param];
  68. return string.IsNullOrEmpty(value) ? null : value.ConvertToInt();
  69. }
  70. /// <summary>
  71. /// Query the string utility. Designed to use inside Web DLL's mostly and legacy support for ASP.NET 2.0.
  72. /// </summary>
  73. /// <param name="param">The param.</param>
  74. /// <returns>Query string param as Nullable long</returns>
  75. /// <example>View code: <br />
  76. /// long? id = Extensions.QueryStringAsLong("id");
  77. /// </example>
  78. public static long? QueryStringAsLong(string param)
  79. {
  80. string value = HttpContext.Current.Request.QueryString[param];
  81. return string.IsNullOrEmpty(value) ? null : value.ConvertToLong();
  82. }
  83. /// <summary>
  84. /// Query the string utility. Designed to use for ASP.NET 3.5.
  85. /// </summary>
  86. /// <param name="request">The HTTP values sent by a client during a Web request.</param>
  87. /// <param name="param">The param.</param>
  88. /// <returns>Query string param as Nullable long</returns>
  89. /// <example>View code: <br />
  90. /// long? id = this.Page.Request.QueryStringAsLong("id");
  91. /// </example>
  92. public static long? QueryStringAsLong(this HttpRequest request, string param)
  93. {
  94. string value = request.QueryString[param];
  95. return string.IsNullOrEmpty(value) ? null : value.ConvertToLong();
  96. }
  97. /// <summary>
  98. /// Query the string utility. Designed to use inside Web DLL's mostly and legacy support for ASP.NET 2.0.
  99. /// </summary>
  100. /// <param name="param">The param.</param>
  101. /// <returns>Query string param as Nullable double</returns>
  102. /// <example>View code: <br />
  103. /// Double? id = Extensions.QueryStringAsDouble("id");
  104. /// </example>
  105. public static double? QueryStringAsDouble(string param)
  106. {
  107. string value = HttpContext.Current.Request.QueryString[param];
  108. return string.IsNullOrEmpty(value) ? null : value.ConvertToDouble();
  109. }
  110. /// <summary>
  111. /// Query the string utility. Designed to use for ASP.NET 3.5.
  112. /// </summary>
  113. /// <param name="request">The HTTP values sent by a client during a Web request</param>
  114. /// <param name="param">The param.</param>
  115. /// <returns>Query string param as Nullable double</returns>
  116. /// <example>View code: <br />
  117. /// double? id = this.Page.Request.QueryStringAsDouble("id");
  118. /// </example>
  119. public static double? QueryStringAsDouble(this HttpRequest request, string param)
  120. {
  121. string value = request.QueryString[param];
  122. return string.IsNullOrEmpty(value) ? null : value.ConvertToDouble();
  123. }
  124. /// <summary>
  125. /// Query the string utility. Designed to use inside Web DLL's mostly and legacy support for ASP.NET 2.0.
  126. /// </summary>
  127. /// <param name="param">The param.</param>
  128. /// <returns>Query string param as Nullable float</returns>
  129. /// <example>View code: <br />
  130. /// float? id = Extensions.QueryStringAsFloat("id");
  131. /// </example>
  132. public static float? QueryStringAsFloat(string param)
  133. {
  134. string value = HttpContext.Current.Request.QueryString[param];
  135. return string.IsNullOrEmpty(value) ? null : value.ConvertToFloat();
  136. }
  137. /// <summary>
  138. /// Query the string utility. Designed to use for ASP.NET 3.5.
  139. /// </summary>
  140. /// <param name="request">The HTTP values sent by a client during a Web request</param>
  141. /// <param name="param">The param.</param>
  142. /// <returns>Query string param as Nullable float</returns>
  143. /// <example>View code: <br />
  144. /// float? id = this.Page.Request.QueryStringAsFloat("id");
  145. /// </example>
  146. public static float? QueryStringAsFloat(this HttpRequest request, string param)
  147. {
  148. string value = request.QueryString[param];
  149. return string.IsNullOrEmpty(value) ? null : value.ConvertToFloat();
  150. }
  151. /// <summary>
  152. /// Query the string utility. Designed to use inside Web DLL's mostly and legacy support for ASP.NET 2.0.
  153. /// </summary>
  154. /// <param name="param">The param.</param>
  155. /// <returns>Query string param as Nullable DateTime</returns>
  156. /// <example>View code: <br />
  157. /// DateTime? id = Extensions.QueryStringAsDateTime("datetime");
  158. /// </example>
  159. public static DateTime? QueryStringAsDateTime(string param)
  160. {
  161. string value = HttpContext.Current.Request.QueryString[param];
  162. return string.IsNullOrEmpty(value) ? null : value.ConvertToDateTime();
  163. }
  164. /// <summary>
  165. /// Query the string utility. Designed to use for ASP.NET 3.5.
  166. /// </summary>
  167. /// <param name="request">The HTTP values sent by a client during a Web request</param>
  168. /// <param name="param">The param.</param>
  169. /// <returns>Query string param as Nullable DateTime</returns>
  170. /// <example>View code: <br />
  171. /// DateTime? id = this.Page.Request.QueryStringAsDateTime("datetime");
  172. /// </example>
  173. public static DateTime? QueryStringAsDateTime(this HttpRequest request, string param)
  174. {
  175. string value = request.QueryString[param];
  176. return string.IsNullOrEmpty(value) ? null : value.ConvertToDateTime();
  177. }
  178. /// <summary>
  179. /// Query the string utility. Designed to use inside Web DLL's mostly and legacy support for ASP.NET 2.0.
  180. /// </summary>
  181. /// <param name="param">The param.</param>
  182. /// <returns>Query string param as Nullable Boolean</returns>
  183. /// <example>View code: <br />
  184. /// bool? id = Extensions.QueryStringAsBoolean("iscurrent");
  185. /// </example>
  186. public static bool? QueryStringAsBoolean(string param)
  187. {
  188. string value = HttpContext.Current.Request.QueryString[param];
  189. return string.IsNullOrEmpty(value) ? null : value.ConvertToBoolean();
  190. }
  191. /// <summary>
  192. /// Query the string utility. Designed to use for ASP.NET 3.5.
  193. /// </summary>
  194. /// <param name="request">The HTTP values sent by a client during a Web request</param>
  195. /// <param name="param">The param.</param>
  196. /// <returns>Query string param as Nullable Boolean</returns>
  197. /// <example>View code: <br />
  198. /// bool? id = this.Page.Request.QueryStringAsBoolean("iscurrent");
  199. /// </example>
  200. public static bool? QueryStringAsBoolean(this HttpRequest request, string param)
  201. {
  202. string value = request.QueryString[param];
  203. return string.IsNullOrEmpty(value) ? null : value.ConvertToBoolean();
  204. }
  205. /// <summary>
  206. /// Query the string utility. Designed to use inside Web DLL's mostly and legacy support for ASP.NET 2.0.
  207. /// </summary>
  208. /// <param name="param">The param.</param>
  209. /// <returns>Query string param as Nullable Guid</returns>
  210. /// <example>View code: <br />
  211. /// Guid? id = Extensions.QueryStringAsGuid("id");
  212. /// </example>
  213. public static Guid? QueryStringAsGuid(string param)
  214. {
  215. string value = HttpContext.Current.Request.QueryString[param];
  216. return string.IsNullOrEmpty(value) ? null : value.ConvertToGuid();
  217. }
  218. /// <summary>
  219. /// Query the string utility.Designed to use for ASP.NET 3.5.
  220. /// </summary>
  221. /// <param name="request">The HTTP values sent by a client during a Web request</param>
  222. /// <param name="param">The param.</param>
  223. /// <returns>Query string param as Nullable Guid</returns>
  224. /// <example>View code: <br />
  225. /// Guid? id = this.Page.Request.QueryStringAsGuid("id");
  226. /// </example>
  227. public static Guid? QueryStringAsGuid(this HttpRequest request, string param)
  228. {
  229. string value = request.QueryString[param];
  230. return string.IsNullOrEmpty(value) ? null : value.ConvertToGuid();
  231. }
  232. }
  233. }