Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

189 строки
8.3KB

  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. ' JSON API requests get a JSON 500 (never an HTML snippet with status 200); details only in development.
  110. If LCase(Left(GetRequestPath(), 5)) = "/api/" Then
  111. Dim msg
  112. msg = "Internal error. The request was not completed."
  113. If isDevelopment Then
  114. msg = msg & " [dev] " & actionName & ": " & errorDesc & " (" & errorNum & ")"
  115. End If
  116. On Error Resume Next
  117. DAL().RollbackTransaction
  118. Err.Clear
  119. On Error GoTo 0
  120. console.error "{""log"":""action_error"",""request_id"":" & JsonLite_Quote(Api_RequestId()) & ",""action"":" & JsonLite_Quote(actionName) & ",""error"":" & JsonLite_Quote(errorNum & " " & errorDesc) & "}"
  121. Api_Error 500, "internal_error", msg
  122. Exit Sub
  123. End If
  124. If isDevelopment Then
  125. Response.Write "<div style='padding:15px; margin:10px; border:2px solid #dc3545; background:#f8d7da; color:#721c24; border-radius:4px;'>"
  126. Response.Write "<strong>Controller Action Error</strong><br>"
  127. Response.Write "Action: <code>" & Server.HTMLEncode(actionName) & "</code><br>"
  128. Response.Write "Error: " & Server.HTMLEncode(errorDesc) & "<br>"
  129. Response.Write "Error Number: " & errorNum
  130. Response.Write "</div>"
  131. Else
  132. Response.Write "<div style='padding:15px; margin:10px; border:2px solid #dc3545; background:#f8d7da; color:#721c24; border-radius:4px;'>"
  133. Response.Write "<strong>An error occurred</strong><br>"
  134. Response.Write "Please contact the system administrator if the problem persists."
  135. Response.Write "</div>"
  136. End If
  137. End Sub
  138. Public Sub RequirePost
  139. If Request.Form.Count = 0 Then MVC.RedirectToExt "NotValid","",empty:End If
  140. End Sub
  141. ' Shortcut for RedirectToActionExt that does not require passing a parameters argument.
  142. Public Sub RedirectToAction(ByVal action_name)
  143. RedirectToActionExt action_name, empty
  144. End Sub
  145. Public Sub RedirectTo(controller_name, action_name)
  146. RedirectToExt controller_name, action_name, empty
  147. End Sub
  148. ' Redirects the browser to the specified action on the specified controller with the specified querystring parameters.
  149. ' params is a KVArray of querystring parameters.
  150. Public Sub RedirectToExt(controller_name, action_name, params)
  151. Response.Redirect Routes.UrlTo(controller_name, action_name, params)
  152. End Sub
  153. Public Sub RedirectToActionExt(ByVal action_name, ByVal params)
  154. RedirectToExt ControllerName, action_name, params
  155. End Sub
  156. End Class
  157. dim MVC_Dispatcher_Class__Singleton
  158. Function MVC()
  159. if IsEmpty(MVC_Dispatcher_Class__Singleton) then
  160. set MVC_Dispatcher_Class__Singleton = new MVC_Dispatcher_Class
  161. end if
  162. set MVC = MVC_Dispatcher_Class__Singleton
  163. End Function
  164. %>

Powered by TurnKey Linux.