保证ASP.NET控制器使用每个请求只有一次? [英] Are ASP.NET controllers guaranteed to be used only once per request?

查看:102
本文介绍了保证ASP.NET控制器使用每个请求只有一次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们想创建通过在控制器的所有动作方法被用于一些对象,我们可以将其存储在控制器的实例变量?

If we want to create some objects to be used by all action methods in a controller, can we store them as instance variables in the controller?

菲尔哈克提到,控制器并不意味着在这个旧后可以重复使用:
<一href=\"http://stackoverflow.com/questions/222300/asp-net-mvc-beta-$p$pvious-routedata-overrides-current-routedata\">http://stackoverflow.com/questions/222300/asp-net-mvc-beta-$p$pvious-routedata-overrides-current-routedata

Phil Haack mentions that controllers are not meant to be reused in this old post: http://stackoverflow.com/questions/222300/asp-net-mvc-beta-previous-routedata-overrides-current-routedata

但是,这是一个控制器,每个请求的行为保证?

But is this one-controller-per-request behavior guaranteed?

我不希望在一个重用控制器从另一个请求有数据的情况。

I don't want to be in a situation where a reused controller has data from another request.

推荐答案

是(在你的标题中的问题)不和(在您的文章中的问题)。

Yes (to the question in your title) and No (to the question in your post).

一个新的控制器实例为每个请求创建。你也许可以用你自己的控制器工厂,缓存控制器,要做到这一点,只有创建它们作为必要的,但我不会推荐它。你会关闭简单缓存需要无论是在会议或高速缓存或存储在查看信息的任何信息(隐藏,如果需要的话),而不是去创造一个新的控制器工厂的麻烦越好。

A new controller instance is created for each request. You might be able to do this by using your own controller factory that caches controllers and only creates them as needed, but I wouldn't recommend it. You'd be better off to simply cache any information that is needed either in the Session or the Cache or store the information in the View (hidden, if necessary) than to go to the trouble of creating a new controller factory.

这篇关于保证ASP.NET控制器使用每个请求只有一次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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