Asp.net mvc ASP.NET MVC基本控制器功能在需要时不启动
我注意到一些我不喜欢的行为,我想知道这是否正常 我有一个从Mvc.Controller继承的BaseController,我有一个视图函数,它可以在每次页面加载时启动Asp.net mvc ASP.NET MVC基本控制器功能在需要时不启动,asp.net-mvc,actionresult,Asp.net Mvc,Actionresult,我注意到一些我不喜欢的行为,我想知道这是否正常 我有一个从Mvc.Controller继承的BaseController,我有一个视图函数,它可以在每次页面加载时启动 Protected Overrides Function View(ByVal viewName As String, ByVal masterName As String, ByVal model As Object) As System.Web.Mvc.ViewResult If Session("UserInfo
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
我遇到的问题是,如果会话状态为closed,则此函数会出现对象引用错误
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
实际上是在returnview(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
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
这是放置会话状态“stuff”的正确位置吗?我将在编辑中用解决方案将其标记为答案