25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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

Powered by TurnKey Linux.