我注意到一些我不喜欢的行为,我想知道这是否正常。
我有一个继承自Mvc.Controller的BaseController,我内部有一个View函数,可以在每个页面加载时触发。
Protected Overrides Function View(ByVal viewName As String, ByVal masterName As String, ByVal model As Object) As System.Web.Mvc.ViewResult
If Session("UserInfo") Is Nothing AndAlso User.Identity.IsAuthenticated Then
Try
ActivityMonitorService.AddActivity(UserService.GetUserByOpenID(HttpContext.User.Identity.Name).ID, _
Utilities.ActivityMonitor.Log.SessionStarted, _
Utilities.DataConverters.IPAddressToNumber(HttpContext.Request.UserHostAddress))
ActivityMonitorService.SubmitChanges()
Catch : End Try
Session("UserInfo") = UserService.GetUserByOpenID(HttpContext.User.Identity.Name)
End If
End Function
我遇到的问题是如果会话状态关闭,我会在此函数上出现对象引用错误
Function Edit(ByVal id As Integer) As ActionResult
If DirectCast(Session("UserInfo"), Domain.User).ID = id Then
Dim user As Domain.User = UserService.GetUserByID(id)
Return View(user)
Else
Response.StatusCode = CInt(HttpStatusCode.NotFound)
Return RedirectToAction("NotFound", "Error")
End If
End Function
现在我假设这是因为基函数View
实际上是在Return View(user)
上启动的。如果这是真的,我将如何连接它以在ActionResult
呼叫中发起事件?
如果我把代码放在这里似乎确实有效
Protected Overrides Function CreateActionInvoker() As System.Web.Mvc.IActionInvoker
If Session("UserInfo") Is Nothing AndAlso User.Identity.IsAuthenticated Then
Try
ActivityMonitorService.AddActivity(UserService.GetUserByOpenID(HttpContext.User.Identity.Name).ID, _
Utilities.ActivityMonitor.Log.SessionStarted, _
Utilities.DataConverters.IPAddressToNumber(HttpContext.Request.UserHostAddress))
ActivityMonitorService.SubmitChanges()
Catch : End Try
Session("UserInfo") = UserService.GetUserByOpenID(HttpContext.User.Identity.Name)
End If
Return MyBase.CreateActionInvoker()
End Function
这是将会话状态设置为“东西”的正确位置吗?
答案 0 :(得分:0)
我打算将此标记为我的编辑
中的解决方案的答案Protected Overrides Function CreateActionInvoker() As System.Web.Mvc.IActionInvoker
If Session("UserInfo") Is Nothing AndAlso User.Identity.IsAuthenticated Then
Try
ActivityMonitorService.AddActivity(UserService.GetUserByOpenID(HttpContext.User.Identity.Name).ID, _
Utilities.ActivityMonitor.Log.SessionStarted, _
Utilities.DataConverters.IPAddressToNumber(HttpContext.Request.UserHostAddress))
ActivityMonitorService.SubmitChanges()
Catch : End Try
Session("UserInfo") = UserService.GetUserByOpenID(HttpContext.User.Identity.Name)
End If
Return MyBase.CreateActionInvoker()
End Function