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.

lib.MVC.asp 6.6KB

il y a 8 mois
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <%
  2. Response.ExpiresAbsolute = "2000-01-01"
  3. Response.AddHeader "pragma", "no-cache"
  4. Response.AddHeader "cache-control", "private, no-cache, must-revalidate"
  5. '=======================================================================================================================
  6. ' MVC Dispatcher
  7. '=======================================================================================================================
  8. Class MVC_Dispatcher_Class
  9. Private m_controller_name
  10. Private m_action_name
  11. Private m_default_action_name
  12. Private m_action_params
  13. Private m_controller_instance
  14. Private m_is_partial
  15. Private Sub Class_initialize
  16. m_default_action_name = "Index"
  17. SetControllerActionNames
  18. 'SetActionParams
  19. End Sub
  20. Public Property Get ControllerName
  21. ControllerName = m_controller_name
  22. end Property
  23. Public Property Get ActionName
  24. ActionName = m_action_name
  25. end Property
  26. Public Property Get IsPartial
  27. IsPartial = m_is_partial
  28. End Property
  29. 'Public Property Get ActionParams
  30. ' if IsEmpty(m_action_params) then set m_action_params = Server.CreateObject("Scripting.Dictionary")
  31. ' set ActionParams = m_action_params
  32. 'end Property
  33. '---------------------------------------------------------------------------------------------------------------------
  34. ' Instantiates the controller and executes the requested action on the controller.
  35. Public Sub Dispatch
  36. dim class_name : class_name = m_controller_name & "Controller"
  37. 'set the global controller reference
  38. executeglobal "dim Controller : set Controller = new " & class_name
  39. If Request.Querystring("_P").Count = 1 then ' = 1 Or Request.Querystring("_P") = "true" then
  40. m_is_partial = true
  41. Else
  42. m_is_partial = false
  43. End If
  44. If Not IsPartial then
  45. %>
  46. <!--#include file="../App/Views/Shared/layout.header.asp"-->
  47. <%
  48. End If
  49. ExecuteAction ActionName
  50. If Not IsPartial then
  51. %>
  52. <!--#include file="../App/Views/Shared/layout.footer.asp"-->
  53. <%
  54. End If
  55. End Sub
  56. '---------------------------------------------------------------------------------------------------------------------
  57. ' Executes the requested action on the current already-instantiated controller
  58. Private Sub ExecuteAction(action_name)
  59. ' no longer want to pass Request.Form as a parameter to actions -- allows actions to be called any way, more flexibility
  60. Execute "Controller." & action_name
  61. End Sub
  62. '---------------------------------------------------------------------------------------------------------------------
  63. ' Ensures an action request comes in via HTTP POST only. Raises error if not.
  64. Public Sub RequirePost
  65. If Request.Form.Count = 0 Then Err.Raise 1, "MVC_Helper_Class:RequirePost", "Action only responds to POST requests."
  66. End Sub
  67. '---------------------------------------------------------------------------------------------------------------------
  68. Public Sub RedirectTo(controller_name, action_name)
  69. RedirectToExt controller_name, action_name, empty
  70. End Sub
  71. ' Redirects the browser to the specified action on the specified controller with the specified querystring parameters.
  72. ' params is a KVArray of querystring parameters.
  73. Public Sub RedirectToExt(controller_name, action_name, params)
  74. Response.Redirect Routes.UrlTo(controller_name, action_name, params)
  75. End Sub
  76. ' Shortcut for RedirectToActionExt that does not require passing a parameters argument.
  77. Public Sub RedirectToAction(ByVal action_name)
  78. RedirectToActionExt action_name, empty
  79. End Sub
  80. ' Redirects the browser to the specified action in the current controller, passing the included parameters.
  81. ' params is a KVArray of querystring parameters.
  82. Public Sub RedirectToActionExt(ByVal action_name, ByVal params)
  83. RedirectToExt ControllerName, action_name, params
  84. End Sub
  85. ' Redirects to the specified action using a form POST.
  86. Public Sub RedirectToActionPOST(action_name)
  87. RedirectToActionExtPOST action_name, empty
  88. End Sub
  89. ' Redirects to the specified action name on the current controller using a form post
  90. Public Sub RedirectToActionExtPOST(action_name, params)
  91. put "<form id='mvc_redirect_to_action_post' action='" & Routes.UrlTo(ControllerName, action_name, empty) & "' method='POST'>"
  92. put "<input type='hidden' name='mvc_redirect_to_action_post_flag' value='1'>"
  93. if Not IsEmpty(params) then
  94. dim i, key, val
  95. for i = 0 to ubound(params) step 2
  96. KeyVal params, i, key, val
  97. put "<input type='hidden' name='" & key & "' value='" & val & "'>"
  98. next
  99. end if
  100. put "</form>"
  101. put "<script type='text/javascript'>"
  102. put "$('#mvc_redirect_to_action_post').submit();"
  103. put "</script>"
  104. End Sub
  105. '---------------------------------------------------------------------------------------------------------------------
  106. ' PRIVATE
  107. '---------------------------------------------------------------------------------------------------------------------
  108. Private Sub SetControllerActionNames
  109. dim full_path : full_path = request.servervariables("path_info")
  110. dim part_path : part_path = split(full_path, Routes.ControllersUrl)(1)
  111. dim part_path_split : part_path_split = split(part_path, "/")
  112. m_controller_name = part_path_split(0)
  113. m_action_name = Choice(request("_A") <> "", request("_A"), m_default_action_name)
  114. End Sub
  115. ' This is deprecated to avoid creating a Dictionary object with every request.
  116. ' Hasn't been used in forever anyway.
  117. 'Private Sub SetActionParams
  118. ' dim key, val
  119. ' 'set m_action_params = Server.CreateObject("scripting.dictionary")
  120. ' for each key in request.querystring
  121. ' val = request.querystring(key)
  122. ' 'ignore service keys
  123. ' if instr(1, "_A", key, 1) = 0 then
  124. ' ActionParams.add key, CStr(val)
  125. ' end if
  126. ' next
  127. 'End Sub
  128. end Class
  129. dim MVC_Dispatcher_Class__Singleton
  130. Function MVC()
  131. if IsEmpty(MVC_Dispatcher_Class__Singleton) then
  132. set MVC_Dispatcher_Class__Singleton = new MVC_Dispatcher_Class
  133. end if
  134. set MVC = MVC_Dispatcher_Class__Singleton
  135. End Function
  136. %>

Powered by TurnKey Linux.