asp.net-mvc – 我应该在ASP.NET MVC3中附加自定义用户上下文会话包装器?

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 我应该在ASP.NET MVC3中附加自定义用户上下文会话包装器?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经在MVC中阅读了许多关于会话范围数据的帖子,但我仍然不清楚在适当的地方将定制的会话包装器包含在解决方案中。

我想从IPrincipal获取当前用户用户名,加载该用户的其他信息,并将其存储在会话中。然后我想从Controller和View访问该用户数据。

以下方法似乎都不符合我想要做的事情。

选项1:直接访问会话集合

每个人似乎都同意this is a bad idea,但老实说,它似乎是最简单的事情。但是,它不会使用户对视图可用。

  1. public class ControllerBase : Controller {
  2. public ControllerBase() : this(new UserRepository()) {}
  3. public ControllerBase(IUserRepository userRepository) {
  4. _userRepository = userRepository;
  5. }
  6. protected IUserRepository _userRepository = null;
  7. protected const string _userSessionKey = "ControllerBase_UserSessionKey";
  8. protected User {
  9. get {
  10. var user = HttpContext.Current.Session[_userSessionKey] as User;
  11. if (user == null) {
  12. var principal = this.HttpContext.User;
  13. if (principal != null) {
  14. user = _userRepository.LoadByName(principal.Identity.Name);
  15. HttpContext.Current.Session[_userSessionKey] = user;
  16. }
  17. }
  18. return user;
  19. }
  20. }
  21. }

选项2:将会话注入类构造函数forum post

这个选项似乎相当不错,但我仍然不确定如何将它附加到Controller和View。我可以在控制器中新增,但不应该作为依赖注入?

  1. public class UserContext {
  2. public UserContext()
  3. : this(new HttpSessionStateWrapper(HttpContext.Current.Session),new UserRepository()) { }
  4.  
  5. public UserContext(HttpSessionStateBase sessionWrapper,IUserRepository userRepository) {
  6. Session = sessionWrapper;
  7. UserRepository = userRepository;
  8. }
  9.  
  10. private HttpSessionStateBase Session { get; set; }
  11. private IUserRepository UserRepository{ get; set; }
  12.  
  13. public User Current {
  14. get {
  15. //see same code as option one
  16. }
  17. }
  18. }

选项3:使用Brad Wilson的StatefulStorage类

他的presentation年Brad Wilson拥有他的StatefulStorage类。它是一个聪明有用的类,包括接口和使用构造函数注入。但是,它似乎导致我与选项2相同的路径。它使用接口,但是我不能使用容器来注入它,因为它依赖于静态工厂。即使我可以注入它,它如何传递给视图。每个viewmodel都必须有一个可设置的User属性的基类?

选项4:使用类似于Hanselman IPrincipal ModelBinder内容

我可以将User作为参数添加到Action方法中,并使用ModelBinder从Session中进行合并。这似乎是很多开销添加到它需要的地方。此外,我还是必须将其添加viewmodel以使其可用于View。

  1. public ActionResult Edit(int id,[ModelBinder(typeof(IPrincipalModelBinder))] IPrincipal user)
  2. { ... }

我觉得我是在反思这个,但似乎应该有一个明显的地方去做这样的事情。我失踪了什么

解决方法

我的会话方法

封面会话界面:

  1. public interface ISessionWrapper
  2. {
  3. int SomeInteger { get; set; }
  4. }

使用HttpContext.Current.Session实现接口:

  1. public class HttpContextSessionWrapper : ISessionWrapper
  2. {
  3. private T GetFromSession<T>(string key)
  4. {
  5. return (T) HttpContext.Current.Session[key];
  6. }
  7.  
  8. private void SetInSession(string key,object value)
  9. {
  10. HttpContext.Current.Session[key] = value;
  11. }
  12.  
  13. public int SomeInteger
  14. {
  15. get { return GetFromSession<int>("SomeInteger"); }
  16. set { SetInSession("SomeInteger",value); }
  17. }
  18. }

注入控制器:

  1. public class BaseController : Controller
  2. {
  3. public ISessionWrapper SessionWrapper { get; set; }
  4.  
  5. public BaseController(ISessionWrapper sessionWrapper)
  6. {
  7. SessionWrapper = sessionWrapper;
  8. }
  9. }

Ninject依赖:

  1. Bind<ISessionWrapper>().To<HttpContextSessionWrapper>()

当您要在母版页中使用ViewData并在特定视图中使用视图模型时,可以使用ViewData传递一些常用信息。

猜你在找的asp.Net相关文章