ASP.NET MVC 基本控制器功能在我需要时没有触发
我注意到一些我不喜欢的行为,我想知道这是否正常。
我有一个继承自 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(用户)
。如果这是真的,我将如何连接它以在每次 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
这是放置会话状态“东西”的正确位置吗?
I'm noticing some behavior that I don't like, and I'm wondering if this is normal.
I've got a BaseController that is Inheriting from Mvc.Controller, and I've got a View function inside that is to fire on every page load.
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
The Problem I have is if the Session state is closed, I get an Object Reference error on this 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
Now I'm assuming it is because the Base Function View
is actually firing on Return View(user)
. If this is true, how would I wire it up to fire an event on ever ActionResult
Call?
EDIT:
It does appear to work however if I put the code here
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
Is this the right place to put the Session State "stuff"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将在我的编辑中使用解决方案将此标记为答案
I'm going to mark this as answer with the solution in my Edit