ASP.NET MVC 基本控制器功能在我需要时没有触发

发布于 2024-09-07 10:11:51 字数 2284 浏览 2 评论 0原文

我注意到一些我不喜欢的行为,我想知道这是否正常。

我有一个继承自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝礼 2024-09-14 10:11:51

我将在我的编辑中使用解决方案将此标记为答案

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 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文