更改ASP.NET中的默认会话提供程序(Change default session provider in ASP.NET)

编程入门 行业动态 更新时间:2024-10-24 12:24:51
更改ASP.NET中的默认会话提供程序(Change default session provider in ASP.NET)

我想改变我的会话被提示为静态类型 - 我只是讨厌键入字符串,因为我做了很多错误。

我用的是什么技术? ASP.NET MVC通过EXT.NET MVC

我试图使用web.config来做到这一点,但问题是,在向其添加会话状态后,visual不会编译我的代码,因为该会话应该使用字符串作为键。

我想通过以下枚举使用会话:

public enum SessionEnum{Model} public class Bar{ void foo(){ Session[SessionEnum.Model] = "blah"; } }

我知道我可以创建包装器将枚举转换为字符串,但对我来说这不是一个非常令人满意的解决方案。

public class StorageWrapper{ public object this[SessionEnum enum]{ get{return Session[enum.toString()]}; //+set }

我所做的是为我的所有控制器创建基类的静态对象然后我能够在它们之间使用它但是在关闭并再次打开页面之后我无法从中获取值。 我想我应该以某种方式序列化它们,但我不知道如何。

有没有办法做到这一点?

编辑我的会话现在看起来像这样:

[Serializable] public abstract class DataWrapper<T> : HttpSessionStateBase { Dictionary<T, object> Dictionary { get; set; } = new Dictionary<T, object>(); public object this[T a] { get { try { return Dictionary[a]; } catch { return null; } } set { Dictionary[a] = value; } } } [Serializable] public class SessionWrapper : DataWrapper<SessionNames> {} public enum SessionNames { Model, Login, LastOpenedFile }

I want to change my session proviced to statically typed - I just hate typing strings because of many many errors I do.

What technology am I using? ASP.NET MVC via EXT.NET MVC

I was trying to do that using web.config but the problem is that after add session state to it visual is not going to compile my code because of that session should be using strings as keys.

I want to use session by enums such as :

public enum SessionEnum{Model} public class Bar{ void foo(){ Session[SessionEnum.Model] = "blah"; } }

I am aware that I can create wrapper converting enums to strings but it's not very satisfying solution for me.

public class StorageWrapper{ public object this[SessionEnum enum]{ get{return Session[enum.toString()]}; //+set }

What I did was create static object for base class for all of my controllers and then I was able to use it across them but after closing and opening the page again I wasn't able to get values from it. I guess I should serialize them somehow but I have no idea how.

Is there any way to do that?

EDIT My session now looks like this :

[Serializable] public abstract class DataWrapper<T> : HttpSessionStateBase { Dictionary<T, object> Dictionary { get; set; } = new Dictionary<T, object>(); public object this[T a] { get { try { return Dictionary[a]; } catch { return null; } } set { Dictionary[a] = value; } } } [Serializable] public class SessionWrapper : DataWrapper<SessionNames> {} public enum SessionNames { Model, Login, LastOpenedFile }

最满意答案

这很简单。 创建一个UserSession对象,它可以执行您想要的所有操作(将您的值保存为枚举等),实例化它,然后将其放入会话中。

var US = new UserSession(); US.stuff = somestuff; Session["UserSess"] = US

然后你可以随时使用Session["UserSess"].stuff

It's very simple. Create a UserSession object which does everything you want (holds your values as enum etc), instantiate it, then put it in the session.

var US = new UserSession(); US.stuff = somestuff; Session["UserSess"] = US

Then you can just always use Session["UserSess"].stuff;

更多推荐

本文发布于:2023-08-05 00:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1423306.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:程序   NET   ASP   Change   provider

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!