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.

166 lignes
7.2KB

  1. <!--#include file="../app/Controllers/autoload_controllers.asp" -->
  2. <%
  3. ' Set cache expiration from configuration
  4. Dim cacheYear : cacheYear = GetAppSetting("CacheExpirationYear")
  5. If cacheYear = "nothing" Then cacheYear = "2030"
  6. Response.ExpiresAbsolute = "01/01/" & cacheYear
  7. Response.AddHeader "pragma", "no-cache"
  8. Response.AddHeader "cache-control", "private, no-cache, must-revalidate"
  9. '=======================================================================================================================
  10. ' MVC Dispatcher
  11. '=======================================================================================================================
  12. Class MVC_Dispatcher_Class
  13. dim CurrentController
  14. Public Property Get ControllerName
  15. ControllerName = CurrentController
  16. end Property
  17. '---------------------------------------------------------------------------------------------------------------------
  18. ' Convenience method to resolve route and dispatch in one call
  19. ' method: HTTP method (GET, POST, etc.)
  20. ' path: Request path (already cleaned of query params)
  21. '---------------------------------------------------------------------------------------------------------------------
  22. Public Sub DispatchRequest(method, path)
  23. Dim routeArray
  24. routeArray = router.Resolve(method, path)
  25. Dispatch routeArray
  26. End Sub
  27. '---------------------------------------------------------------------------------------------------------------------
  28. ' Main dispatch method - executes a resolved route
  29. ' RouteArray: Array(controller, action, params) from router.Resolve()
  30. '---------------------------------------------------------------------------------------------------------------------
  31. Public Sub Dispatch(RouteArray)
  32. On Error Resume Next
  33. Dim controllerName, actionName, hasParams, paramsArray
  34. controllerName = RouteArray(0)
  35. actionName = RouteArray(1)
  36. ' Security: Validate controller and action names
  37. If Not ControllerRegistry.IsValidControllerFormat(controllerName) Then
  38. Response.Write "<div style='padding:15px; margin:10px; border:2px solid #dc3545; background:#f8d7da; color:#721c24; border-radius:4px;'>"
  39. Response.Write "<strong>Security Error:</strong> Invalid controller name format."
  40. Response.Write "</div>"
  41. Exit Sub
  42. End If
  43. If Not ControllerRegistry.IsValidActionFormat(actionName) Then
  44. Response.Write "<div style='padding:15px; margin:10px; border:2px solid #dc3545; background:#f8d7da; color:#721c24; border-radius:4px;'>"
  45. Response.Write "<strong>Security Error:</strong> Invalid action name format."
  46. Response.Write "</div>"
  47. Exit Sub
  48. End If
  49. ' Security: Check controller whitelist
  50. If Not ControllerRegistry.IsValidController(controllerName) Then
  51. Response.Write "<div style='padding:15px; margin:10px; border:2px solid #dc3545; background:#f8d7da; color:#721c24; border-radius:4px;'>"
  52. Response.Write "<strong>Security Error:</strong> Controller '" & Server.HTMLEncode(controllerName) & "' is not registered."
  53. Response.Write "</div>"
  54. Exit Sub
  55. End If
  56. ' Initialize current controller
  57. Dim controllerAssignment : controllerAssignment = "Set CurrentController = " & controllerName & "()"
  58. Execute controllerAssignment
  59. ' Check if layout should be used
  60. hasParams = (UBound(RouteArray) >= 2)
  61. If eval(controllerName & ".useLayout") Then
  62. %> <!-- #include file="../app/views/Shared/Header.asp" --> <%
  63. End If
  64. ' Prepare parameters
  65. If hasParams Then
  66. paramsArray = SurroundStringInArray(RouteArray(2))
  67. Else
  68. paramsArray = Empty
  69. End If
  70. ' Execute controller action
  71. ExecuteControllerAction controllerName, actionName, paramsArray
  72. ' Include footer if layout is used
  73. If eval(controllerName & ".useLayout") Then
  74. %> <!-- #include file="../app/views/Shared/Footer.asp" --> <%
  75. End If
  76. On Error GoTo 0
  77. End Sub
  78. ' Helper method to execute controller actions (eliminates code duplication)
  79. Private Sub ExecuteControllerAction(controllerName, actionName, paramsArray)
  80. On Error Resume Next
  81. Dim callString
  82. ' Build the call string based on whether we have parameters
  83. If Not IsEmpty(paramsArray) And IsArray(paramsArray) And UBound(paramsArray) >= 0 Then
  84. callString = "Call " & controllerName & "." & actionName & "(" & Join(paramsArray, ",") & ")"
  85. Else
  86. callString = "Call " & controllerName & "." & actionName & "()"
  87. End If
  88. ' Execute the action
  89. Execute callString
  90. ' Handle errors
  91. If Err.Number <> 0 Then
  92. HandleDispatchError actionName, Err.Description, Err.Number
  93. Err.Clear
  94. End If
  95. On Error GoTo 0
  96. End Sub
  97. ' Centralized error handling for dispatch errors
  98. Private Sub HandleDispatchError(actionName, errorDesc, errorNum)
  99. Dim isDevelopment
  100. isDevelopment = (LCase(GetAppSetting("Environment")) = "development")
  101. If isDevelopment Then
  102. Response.Write "<div style='padding:15px; margin:10px; border:2px solid #dc3545; background:#f8d7da; color:#721c24; border-radius:4px;'>"
  103. Response.Write "<strong>Controller Action Error</strong><br>"
  104. Response.Write "Action: <code>" & Server.HTMLEncode(actionName) & "</code><br>"
  105. Response.Write "Error: " & Server.HTMLEncode(errorDesc) & "<br>"
  106. Response.Write "Error Number: " & errorNum
  107. Response.Write "</div>"
  108. Else
  109. Response.Write "<div style='padding:15px; margin:10px; border:2px solid #dc3545; background:#f8d7da; color:#721c24; border-radius:4px;'>"
  110. Response.Write "<strong>An error occurred</strong><br>"
  111. Response.Write "Please contact the system administrator if the problem persists."
  112. Response.Write "</div>"
  113. End If
  114. End Sub
  115. Public Sub RequirePost
  116. If Request.Form.Count = 0 Then MVC.RedirectToExt "NotValid","",empty:End If
  117. End Sub
  118. ' Shortcut for RedirectToActionExt that does not require passing a parameters argument.
  119. Public Sub RedirectToAction(ByVal action_name)
  120. RedirectToActionExt action_name, empty
  121. End Sub
  122. Public Sub RedirectTo(controller_name, action_name)
  123. RedirectToExt controller_name, action_name, empty
  124. End Sub
  125. ' Redirects the browser to the specified action on the specified controller with the specified querystring parameters.
  126. ' params is a KVArray of querystring parameters.
  127. Public Sub RedirectToExt(controller_name, action_name, params)
  128. Response.Redirect Routes.UrlTo(controller_name, action_name, params)
  129. End Sub
  130. Public Sub RedirectToActionExt(ByVal action_name, ByVal params)
  131. RedirectToExt ControllerName, action_name, params
  132. End Sub
  133. End Class
  134. dim MVC_Dispatcher_Class__Singleton
  135. Function MVC()
  136. if IsEmpty(MVC_Dispatcher_Class__Singleton) then
  137. set MVC_Dispatcher_Class__Singleton = new MVC_Dispatcher_Class
  138. end if
  139. set MVC = MVC_Dispatcher_Class__Singleton
  140. End Function
  141. %>

Powered by TurnKey Linux.