Consolidated ASP Classic MVC framework from best components
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

263 lignes
11KB

  1. <%
  2. '=======================================================================================================================
  3. ' ROUTING HELPER
  4. ' Provides URL generation for RouteKit framework
  5. '=======================================================================================================================
  6. Class Route_Helper_Class
  7. Private m_app_url
  8. Private m_content_url
  9. Private m_stylesheets_url
  10. Private m_js_url
  11. Private m_images_url
  12. Private m_enable_cache_busting
  13. Private m_cache_bust_param_name
  14. Private Sub Class_Initialize()
  15. ' Auto-detect application URL from current request
  16. Dim protocol, host, appPath
  17. protocol = IIf(Request.ServerVariables("HTTPS") = "on", "https://", "http://")
  18. host = Request.ServerVariables("HTTP_HOST")
  19. appPath = Request.ServerVariables("APPL_MD_PATH")
  20. ' Extract virtual directory from IIS path
  21. Dim vdir
  22. vdir = ""
  23. If InStr(appPath, "/LM/W3SVC/") > 0 Then
  24. vdir = Mid(appPath, InStrRev(appPath, "/") + 1)
  25. If vdir <> "ROOT" Then
  26. vdir = "/" & vdir
  27. Else
  28. vdir = ""
  29. End If
  30. End If
  31. m_app_url = protocol & host & vdir & "/"
  32. m_content_url = m_app_url & "content/"
  33. m_stylesheets_url = m_app_url & "css/"
  34. m_js_url = m_app_url & "js/"
  35. m_images_url = m_app_url & "images/"
  36. ' Load cache-busting configuration
  37. Dim cacheBustSetting
  38. cacheBustSetting = GetAppSetting("EnableCacheBusting")
  39. m_enable_cache_busting = (cacheBustSetting = "true" Or cacheBustSetting = "True")
  40. ' Get cache-bust parameter name (default: "v")
  41. m_cache_bust_param_name = GetAppSetting("CacheBustParamName")
  42. If m_cache_bust_param_name = "nothing" Or Len(m_cache_bust_param_name) = 0 Then
  43. m_cache_bust_param_name = "v"
  44. End If
  45. End Sub
  46. '---------------------------------------------------------------------------------------------------------------------
  47. ' Cache-busting token (timestamp-based for uniqueness)
  48. '---------------------------------------------------------------------------------------------------------------------
  49. Public Property Get NoCacheToken
  50. NoCacheToken = CLng(Timer() * 100)
  51. End Property
  52. '---------------------------------------------------------------------------------------------------------------------
  53. ' Check if cache-busting is globally enabled
  54. '---------------------------------------------------------------------------------------------------------------------
  55. Public Property Get CacheBustingEnabled
  56. CacheBustingEnabled = m_enable_cache_busting
  57. End Property
  58. '---------------------------------------------------------------------------------------------------------------------
  59. ' Get/Set cache-busting enabled (can override config at runtime)
  60. '---------------------------------------------------------------------------------------------------------------------
  61. Public Property Let CacheBustingEnabled(value)
  62. m_enable_cache_busting = value
  63. End Property
  64. '---------------------------------------------------------------------------------------------------------------------
  65. ' URL Properties
  66. '---------------------------------------------------------------------------------------------------------------------
  67. Public Property Get AppURL
  68. AppURL = m_app_url
  69. End Property
  70. Public Property Get ContentURL
  71. ContentURL = m_content_url
  72. End Property
  73. Public Property Get StylesheetsURL
  74. StylesheetsURL = m_stylesheets_url
  75. End Property
  76. Public Property Get JsURL
  77. JsURL = m_js_url
  78. End Property
  79. Public Property Get ImagesURL
  80. ImagesURL = m_images_url
  81. End Property
  82. '---------------------------------------------------------------------------------------------------------------------
  83. ' Generate clean URL for controller/action (RouteKit style)
  84. '
  85. ' @param controller_name String Name of controller (without "Controller" suffix)
  86. ' @param action_name String Name of action method
  87. ' @param params_array KV Array Optional key/value pair array for query string
  88. ' @returns String Clean URL like "/controller/action?key=val"
  89. '---------------------------------------------------------------------------------------------------------------------
  90. Public Function UrlTo(controller_name, action_name, params_array)
  91. Dim url, qs
  92. ' Build clean URL: /controller/action
  93. url = m_app_url & LCase(controller_name) & "/" & LCase(action_name)
  94. ' Add query string parameters if provided
  95. qs = TO_Querystring(params_array)
  96. If Len(qs) > 0 Then
  97. url = url & "?" & qs
  98. End If
  99. UrlTo = url
  100. End Function
  101. '---------------------------------------------------------------------------------------------------------------------
  102. ' Generate URL with route parameters (e.g., /users/show/123)
  103. '
  104. ' @param controller_name String Name of controller
  105. ' @param action_name String Name of action method
  106. ' @param route_params Array Route parameters (e.g., Array(123) for ID)
  107. ' @param query_params KV Array Optional query string parameters
  108. ' @returns String URL with route params
  109. '---------------------------------------------------------------------------------------------------------------------
  110. Public Function UrlToWithParams(controller_name, action_name, route_params, query_params)
  111. Dim url, qs, i
  112. ' Build base URL
  113. url = m_app_url & LCase(controller_name) & "/" & LCase(action_name)
  114. ' Append route parameters
  115. If IsArray(route_params) Then
  116. For i = 0 To UBound(route_params)
  117. url = url & "/" & Server.URLEncode(CStr(route_params(i)))
  118. Next
  119. ElseIf Not IsEmpty(route_params) Then
  120. url = url & "/" & Server.URLEncode(CStr(route_params))
  121. End If
  122. ' Add query string parameters if provided
  123. qs = TO_Querystring(query_params)
  124. If Len(qs) > 0 Then
  125. url = url & "?" & qs
  126. End If
  127. UrlToWithParams = url
  128. End Function
  129. '---------------------------------------------------------------------------------------------------------------------
  130. ' Generate URL for static asset with cache-busting
  131. '
  132. ' @param asset_path String Relative path to asset (e.g., "css/site.css")
  133. ' @param use_cache_bust Variant Boolean or Empty. If Empty, uses global setting. If True/False, overrides.
  134. ' @returns String Full URL to asset
  135. '---------------------------------------------------------------------------------------------------------------------
  136. Public Function AssetUrl(asset_path, use_cache_bust)
  137. Dim url, shouldBust
  138. url = m_app_url & asset_path
  139. ' Determine if we should cache-bust
  140. If IsEmpty(use_cache_bust) Then
  141. shouldBust = m_enable_cache_busting
  142. Else
  143. shouldBust = CBool(use_cache_bust)
  144. End If
  145. If shouldBust Then
  146. url = AppendCacheBustParam(url)
  147. End If
  148. AssetUrl = url
  149. End Function
  150. '---------------------------------------------------------------------------------------------------------------------
  151. ' Generate URL for route with optional cache-busting
  152. '
  153. ' @param controller_name String Name of controller
  154. ' @param action_name String Name of action
  155. ' @param params_array KV Array Optional query params
  156. ' @param use_cache_bust Variant Boolean or Empty. If Empty, uses global setting
  157. ' @returns String URL with optional cache-busting
  158. '---------------------------------------------------------------------------------------------------------------------
  159. Public Function UrlToWithCacheBust(controller_name, action_name, params_array, use_cache_bust)
  160. Dim url
  161. url = UrlTo(controller_name, action_name, params_array)
  162. ' Determine if we should cache-bust
  163. Dim shouldBust
  164. If IsEmpty(use_cache_bust) Then
  165. shouldBust = m_enable_cache_busting
  166. Else
  167. shouldBust = CBool(use_cache_bust)
  168. End If
  169. If shouldBust Then
  170. url = AppendCacheBustParam(url)
  171. End If
  172. UrlToWithCacheBust = url
  173. End Function
  174. '---------------------------------------------------------------------------------------------------------------------
  175. ' PRIVATE HELPER METHODS
  176. '---------------------------------------------------------------------------------------------------------------------
  177. Private Function TO_Querystring(the_array)
  178. Dim result, idx
  179. result = ""
  180. If Not IsEmpty(the_array) And IsArray(the_array) Then
  181. ' Process key-value pairs (array with even number of elements)
  182. For idx = LBound(the_array) To UBound(the_array) Step 2
  183. If idx + 1 <= UBound(the_array) Then
  184. result = result & GetParam(the_array, idx)
  185. ' Append & between parameters, but not on the last parameter
  186. If Not (idx >= UBound(the_array) - 1) Then
  187. result = result & "&"
  188. End If
  189. End If
  190. Next
  191. End If
  192. TO_Querystring = result
  193. End Function
  194. Private Function GetParam(params_array, key_idx)
  195. Dim key, val
  196. key = Server.URLEncode(CStr(params_array(key_idx)))
  197. val = Server.URLEncode(CStr(params_array(key_idx + 1)))
  198. GetParam = key & "=" & val
  199. End Function
  200. Private Function AppendCacheBustParam(url)
  201. Dim separator
  202. ' Determine if we need ? or &
  203. If InStr(url, "?") > 0 Then
  204. separator = "&"
  205. Else
  206. separator = "?"
  207. End If
  208. AppendCacheBustParam = url & separator & m_cache_bust_param_name & "=" & NoCacheToken
  209. End Function
  210. End Class
  211. '---------------------------------------------------------------------------------------------------------------------
  212. ' Singleton accessor
  213. '---------------------------------------------------------------------------------------------------------------------
  214. Dim Route_Helper__Singleton
  215. Set Route_Helper__Singleton = Nothing
  216. Function Routes()
  217. If Route_Helper__Singleton Is Nothing Then
  218. Set Route_Helper__Singleton = New Route_Helper_Class
  219. End If
  220. Set Routes = Route_Helper__Singleton
  221. End Function
  222. %>

Powered by TurnKey Linux.