Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

174 rindas
7.5KB

  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. ' A blank route is the router's no-match result.
  37. If Len(Trim(controllerName)) = 0 Or Len(Trim(actionName)) = 0 Then
  38. Response.Status = "404 Not Found"
  39. Response.Write "Not Found"
  40. Exit Sub
  41. End If
  42. ' Security: Validate controller and action names
  43. If Not ControllerRegistry.IsValidControllerFormat(controllerName) 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 controller name format."
  46. Response.Write "</div>"
  47. Exit Sub
  48. End If
  49. If Not ControllerRegistry.IsValidActionFormat(actionName) Then
  50. Response.Write "<div style='padding:15px; margin:10px; border:2px solid #dc3545; background:#f8d7da; color:#721c24; border-radius:4px;'>"
  51. Response.Write "<strong>Security Error:</strong> Invalid action name format."
  52. Response.Write "</div>"
  53. Exit Sub
  54. End If
  55. ' Security: Check controller whitelist
  56. If Not ControllerRegistry.IsValidController(controllerName) Then
  57. Response.Write "<div style='padding:15px; margin:10px; border:2px solid #dc3545; background:#f8d7da; color:#721c24; border-radius:4px;'>"
  58. Response.Write "<strong>Security Error:</strong> Controller '" & Server.HTMLEncode(controllerName) & "' is not registered."
  59. Response.Write "</div>"
  60. Exit Sub
  61. End If
  62. ' Initialize current controller
  63. Dim controllerAssignment : controllerAssignment = "Set CurrentController = " & controllerName & "()"
  64. Execute controllerAssignment
  65. ' Check if layout should be used
  66. hasParams = (UBound(RouteArray) >= 2)
  67. If eval(controllerName & ".useLayout") Then
  68. %> <!-- #include file="../app/views/Shared/Header.asp" --> <%
  69. End If
  70. ' Prepare parameters
  71. If hasParams Then
  72. paramsArray = SurroundStringInArray(RouteArray(2))
  73. Else
  74. paramsArray = Empty
  75. End If
  76. ' Execute controller action
  77. ExecuteControllerAction controllerName, actionName, paramsArray
  78. ' Include footer if layout is used
  79. If eval(controllerName & ".useLayout") Then
  80. %> <!-- #include file="../app/views/Shared/Footer.asp" --> <%
  81. End If
  82. On Error GoTo 0
  83. End Sub
  84. ' Helper method to execute controller actions (eliminates code duplication)
  85. Private Sub ExecuteControllerAction(controllerName, actionName, paramsArray)
  86. On Error Resume Next
  87. Dim callString
  88. ' Build the call string based on whether we have parameters
  89. If Not IsEmpty(paramsArray) And IsArray(paramsArray) And UBound(paramsArray) >= 0 Then
  90. callString = "Call " & controllerName & "." & actionName & "(" & Join(paramsArray, ",") & ")"
  91. Else
  92. callString = "Call " & controllerName & "." & actionName & "()"
  93. End If
  94. ' Execute the action
  95. Execute callString
  96. ' Handle errors
  97. If Err.Number <> 0 Then
  98. HandleDispatchError actionName, Err.Description, Err.Number
  99. Err.Clear
  100. End If
  101. On Error GoTo 0
  102. End Sub
  103. ' Centralized error handling for dispatch errors
  104. Private Sub HandleDispatchError(actionName, errorDesc, errorNum)
  105. Dim isDevelopment
  106. isDevelopment = (LCase(GetAppSetting("Environment")) = "development")
  107. If isDevelopment Then
  108. Response.Write "<div style='padding:15px; margin:10px; border:2px solid #dc3545; background:#f8d7da; color:#721c24; border-radius:4px;'>"
  109. Response.Write "<strong>Controller Action Error</strong><br>"
  110. Response.Write "Action: <code>" & Server.HTMLEncode(actionName) & "</code><br>"
  111. Response.Write "Error: " & Server.HTMLEncode(errorDesc) & "<br>"
  112. Response.Write "Error Number: " & errorNum
  113. Response.Write "</div>"
  114. Else
  115. Response.Write "<div style='padding:15px; margin:10px; border:2px solid #dc3545; background:#f8d7da; color:#721c24; border-radius:4px;'>"
  116. Response.Write "<strong>An error occurred</strong><br>"
  117. Response.Write "Please contact the system administrator if the problem persists."
  118. Response.Write "</div>"
  119. End If
  120. End Sub
  121. Public Sub RequirePost
  122. If Request.Form.Count = 0 Then MVC.RedirectToExt "NotValid","",empty:End If
  123. End Sub
  124. ' Shortcut for RedirectToActionExt that does not require passing a parameters argument.
  125. Public Sub RedirectToAction(ByVal action_name)
  126. RedirectToActionExt action_name, empty
  127. End Sub
  128. Public Sub RedirectTo(controller_name, action_name)
  129. RedirectToExt controller_name, action_name, empty
  130. End Sub
  131. ' Redirects the browser to the specified action on the specified controller with the specified querystring parameters.
  132. ' params is a KVArray of querystring parameters.
  133. Public Sub RedirectToExt(controller_name, action_name, params)
  134. Response.Redirect Routes.UrlTo(controller_name, action_name, params)
  135. End Sub
  136. Public Sub RedirectToActionExt(ByVal action_name, ByVal params)
  137. RedirectToExt ControllerName, action_name, params
  138. End Sub
  139. End Class
  140. dim MVC_Dispatcher_Class__Singleton
  141. Function MVC()
  142. if IsEmpty(MVC_Dispatcher_Class__Singleton) then
  143. set MVC_Dispatcher_Class__Singleton = new MVC_Dispatcher_Class
  144. end if
  145. set MVC = MVC_Dispatcher_Class__Singleton
  146. End Function
  147. %>

Powered by TurnKey Linux.