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.

167 lignes
7.3KB

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

Powered by TurnKey Linux.