所谓旧参数控制器动作 [英] Controller action called with old parameter

查看:73
本文介绍了所谓旧参数控制器动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里我的公司,我们不能实例化一个新请求的控制器。我们必须将其存储在会话,并重新利用它每一次,我知道这是错的,但我们必须保持请求之间的控制器的状态。因此,这里就是我们所做的:

here in my company we can't instantiate a controller for each new request. We have to store it in the session and re-utilize it every time, i know this is wrong, but we have to keep the state of the controller between requests. So here's what we did:

我们创造了这个控制器工厂:

We created this controller factory:

public class SGVControllerFactory : IControllerFactory
    {

        public IController CreateController(RequestContext requestContext, string controllerName)
        {
            string _SessionId = controllerName + "Controller";
            foreach (var _Object in HttpContext.Current.Session)
            {
                if (_Object.ToString() == _SessionId)
                {
                    IController _Controller = (IController)HttpContext.Current.Session[_Object.ToString()];



                    return _Controller;
                }
            }
            return null;
        }


        public SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName)
        {
            return SessionStateBehavior.Default;
        }


        public void ReleaseController(IController controller)
        {
            //We never release our controllers!!!
        }
    }

和我们有这个基础的控制器:

And we have this base controller:

public class SGVController : Controller
    {
        protected override void Execute(RequestContext requestContext)
        {
            if (requestContext == null)
            {
                throw new ArgumentNullException("requestContext");
            }
            if (requestContext.HttpContext == null)
            {
                throw new ArgumentException("Http context is null", "requestContext");
            }

            Initialize(requestContext);

            using (ScopeStorage.CreateTransientScope())
            {                
                ExecuteCore();
                Dispose();
            }
        }

    }

该控制器类从默认的MVC控制器执行不同的唯一一件事是,它不限制自己只是调用一次。

The only thing this controller class does differently from the default MVC controller is that it doesn't limit itself to be called just once.

现在,我的问题是..如果我有这样的动作:

Now, my problem is.. if I have this action:

public JsonResult Foo(string Bar) {
 return Json(new List<string> { Bar, Bar });
}

在'酒吧'参数将跳投有第一次调用行动的价值。我找不到任何解释。请求参数词典有正确的价值观,但动作仍然得到旧值。

The 'Bar' parameter will aways have the value of the first call to the action. I can't find anything that explains that. The request parameter dictionary has the right values, but the action still gets the old value.

推荐答案

您可以尝试通过重写初始化方法有正在处理的新值重新初始化的ValueProvider和TempData的。

You may try to reinit the ValueProvider and the TempData by overriding the Initialize method to have the new values being handled.

public class SGVController : Controller
{

  protected override void Initialize(RequestContext requestContext)
   {
    this.TempData = null;
    this.ValueProvider = null;

    base.Initialize(requestContext);
   }        

    protected override void Execute(RequestContext requestContext)
    {
        if (requestContext == null)
        {
            throw new ArgumentNullException("requestContext");
        }
        if (requestContext.HttpContext == null)
        {
            throw new ArgumentException("Http context is null", "requestContext");
        }

        Initialize(requestContext);

        using (ScopeStorage.CreateTransientScope())
        {                
            ExecuteCore();
            Dispose();
        }
    }
}

希望这会有所帮助,

Hope this will help,

这篇关于所谓旧参数控制器动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆