问题描述
我在寻找的想法如何,我应该在一个ASP.NET MVC应用程序中使用会话?使用masterpages并试着尤其是当刚拿到数据的母版没有绕过控制器。这个问题开始了由我问了很多小的问题,但我设法把它塑造成迄今我还没有实现,但其中之一是有点可行的解决方案。将AP preciate任何反馈。
I'm looking for thoughts on how should I use Session in an ASP.NET MVC application? Especially when using masterpages and tryin to just get the data to the masterpage without bypassing the controller. This question started off by me asking a lot of little questions, but then I managed to mould it into a solution which as yet I have not implemented but one which is somewhat workable. Would appreciate any feedback.
我提出的解决方案又名什么,我即将实现,除非有人说停下来!
我有我的模型库继承模型类 - 它包含由母版所需的信息(只有一个每页视图)某些事情会显示在报头或页脚,以及基于谁是配置驱动的设置登录。
I have my model classes inheriting from ModelBase -- which contains the information needed by the masterpage (there is only one view per page) for certain things it displays in the masthead or footer as well as configuration driven settings based upon who is logged in.
我最好的解决方案如下 - 这里显示为产品页面
My best solution is as follows - shown here for a 'products page':
的假设的:我已经在某些时候已经在会议上坚持某些数据 - 例如也许是 PARTNERID
里面传来的通过一个网关页面,或 currentLoggedInUserEmail
属性或完全吹的对象。
我有一个模型库
类从每一个模型 - 如产品型号
继承
I have a ModelBase
class from which every model - such as ProductModel
inherits
我有一个 MySiteControllerBase
类(从控制器继承) - 这是由 ProductController的
子类
I have a MySiteControllerBase
class (inherits from Controller) - which is subclassed by ProductController
.
在 ProductController的
我的操作方法,我创建的产品视图的模型与'的新产品型号()
。这个模型类本身一无所知会议或如何填充模型库
。它本质上甚至不知道模型库
- 它只是从它继承。我的链接构造函数什么也不做(因为我不想把它传递会话
)。
In my action method in ProductController
I create the model for the product view with 'new ProductModel()'
. This model class itself knows nothing about session or how to populate ModelBase
. It essentially doesn't even know about ModelBase
- it just inherits from it. My chained constructor does nothing (because I don't want to pass it Session
).
我重写查看(...)
在 MySiteControllerBase
为所有需要的模型参数的重载。我检查,看看是否该参数的类型为模型库
,如果是我填充属性,如 PARTNERID
和 currentLoggedInuserEmail
。幸运的是,因为我从控制器
继承的类里面我有会话直接访问
这样我就可以拉他们直离开那里。
I override View(...)
in MySiteControllerBase
for all the overloads that take a model parameter. I check to see if that parameter is of type ModelBase
and if it is I populate the properties such as partnerid
and currentLoggedInuserEmail
. Fortunately because I'm inside a class that inherits from Controller
I have direct access to Session
so i can pull them straight out of there.
这个方法意味着,在模型库
的属性将自动只是我做填充'返回视图(模型)
。然而,有一个明显的问题,如果对产品型号
模型需要访问模型库
定义的东西。它会得到null,因为它不填充呢。
This method means that the properties on ModelBase
are automatically populated just by me doing 'return View(model)'
. However there is an obvious issue if the model for ProductModel
needs to access anything defined on ModelBase
. It's going to get null because it isn't populated yet.
这问题可以通过传递会话来解决
到新产品型号(会话)
这又将传递向上的构造链新的模型库(会话)
。我真的不要的这样的解决方案,虽然becasue我喜欢把一个模型作为pretty哑数据结构,不应该知道任何外部数据构建可言。另一种解决方案可能是只翼它,如果我曾经发现 ProductController的
需要消耗模型库
的定义的东西我只是创建一个方法 MySiteControllerBase.UpdateModelBase(产品型号,会话)
来明确填充它里面的 ProductController的
。我希望这就是清除!
This issue can be solved by passing in Session
to new ProductModel(session)
which would in turn pass it up the constructor chain to new ModelBase(session)
. I really dont like that solution though becasue I like to think of a model as a pretty dumb data structure that shouldn't know about any external data constructs at all. Another solution might be to just wing it, and if i ever find that ProductController
needs to consume anything defined in ModelBase
that I just create a method MySiteControllerBase.UpdateModelBase(productModel, session)
to explicitly populate it inside ProductController
. I hope thats clear!
这浮现在脑海中的其他问题是:
Other questions that come to mind are :
- 什么单元测试?有没有解决会话状态的任何抽象的MVC或者我应该建立自己的?我没有在源$ C $ C为会话并没有什么搜索登场了!
- 如何在MVC / REST / FUL /网址会话跟踪的工作?是否有任何问题,第i饼干关闭,我需要了解?
- 我应该想到届不同于我怎么通常有?
推荐答案
虽然没有什么,在原则上,错在ASP.NET MVC应用程序中使用会话(很好,至少没有什么比在其它ASP中使用更错了。 NET应用程序...),我倾向于觉得它应该是最后的手段,当其他事情不工作。
Although there is nothing, in principle, wrong with using Session in ASP.NET MVC applications (well, at least nothing more wrong than using it in other ASP.NET applications...), I tend to feel it should be a last resort, when other things don't work.
虽然你的问题是,一般,非常写得很好,你不进入任何细节上,你提出的会话存储的内容。这两个例子我在你的问题中发现是:
Although your question is, generally, very well-written, you don't go into any great detail on what you propose to store in Session. The two examples I found in your question are:
- 当前用户的电子邮件
- PARTNERID
用户的电子邮件地址已可从窗体身份验证,如果你正在使用,并且可以添加到尚未支持其他ASP.NET会员供应商。目前还不清楚是什么PARTNERID实际上是,但我怀疑会话是唯一可能的地方存放。
The user's e-mail address is already available from forms authentication, if you are using that, and can be added to other ASP.NET membership providers which don't already support it. It's not clear what partnerid actually is, but I'm skeptical that the Session is the only possible place to store it.
在另一方面,这是完全可能的,你需要存储的东西,你还没有告诉我们这将真的只适合在会话。
On the other hand, it's entirely possible that you need to store stuff you haven't told us about which would really only fit in the session.
所以,你走的太远沿着这条道路之前,请确保其他的解决方案是不是已经提供你需要存储的数据。
So before you go too far down this road, make sure that other solutions are not already available for the data you need to store.
这篇关于我应该如何会话状态融入ASP.NET MVC应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!