Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

173 Zeilen
7.4KB

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

Powered by TurnKey Linux.