开发者

ASP.NET MVC Base Controller Function not firing when I need it to

开发者 https://www.devze.com 2023-01-05 09:06 出处:网络
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 t

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.Identit开发者_StackOverflow社区y.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"?


I'm going to mark this as answer with the solution in my Edit

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

精彩评论

暂无评论...
验证码 换一张
取 消