加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 在Action Filter中不正确地存储实例状态

发布时间:2020-12-16 06:23:53 所属栏目:asp.Net 来源:网络整理
导读:我刚刚将我的项目从ASP.NET MVC1.0升级到ASP.NET MVC4.0 令我真正害怕的一件事就是MVC3.0升级文档中的以下句子 In previous versions of ASP.NET MVC,action filters are create per request except in a few cases. This behavior was never a guaranteed b
我刚刚将我的项目从ASP.NET MVC1.0升级到ASP.NET MVC4.0

令我真正害怕的一件事就是MVC3.0升级文档中的以下句子

In previous versions of ASP.NET MVC,action filters are create per
request except in a few cases. This behavior was never a guaranteed
behavior but merely an implementation detail and the contract for
filters was to consider them stateless. In ASP.NET MVC 3,filters are
cached more aggressively. Therefore,any custom action filters which
improperly store instance state might be broken.

我认为没有简单的方法来测试由不正确存储的实例状态引起的错误.
如果我有一个问题,我非常有信心,我只能在生产中找到它.

不正确存储的实例状态真正意味着什么?

我有这个代码:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
   ProductModel productModel = new ProductModel()
   filterContext.ActionParameters["model"] = productModel;
}

这是不正确存储的实例状态的示例吗?

解决方法

不,你的代码片段没问题.存储不正确的状态将是一些类级属性,例如:

public class StatefulActionFilter : ActionFilter
{
  private readonly IPrincipal currentPrincipal = Thread.CurrentPrincipal;

  public override void OnActionExecuting(ActionExecutingContext filterContext)
  {
    ...
    // Using currentPrincipal here would be bad,since it may refer to the principal of a previous request.
    ...
  }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读