.NET HttpSessionState案例不敏感

使用“InProc”存储的.NET的HttpSessionState似乎将会话变量键值视为不区分大小写。例如:
session["foo"] = 1;
session["Foo"] = 2;
Trace.Write(session["foo"].ToString()); // => 2

这个行为似乎没有文档,所以我想知道它是否只是底层会话存储机制的副作用,或者是由类本身有意实现的。
由于C#将所有其他内容都视为区分大小写,所以对于会话来说,这不是一样的。是什么赋予了?商店类型有差异吗?有没有与VB的向后兼容?

解决方法

HttpSessionState的密钥不区分大小写,以匹配经典ASP Session对象的行为。这使得开发人员更容易将ASP应用程序移植到ASP.NET中,而不会引入细微的区分大小写的问题。

QueryString,Cookies等对象和其他类似于Classic-ASP的内置对象都是相同的不区分大小写的关键字。

ASP.NET正在设计时,我正在微软的IIS团队工作,ASP.NET尽可能地将ASP.NET代码向后兼容到ASP。这并不意味着ASP.NET具有完美的向后兼容性,但每当一个决定出现(像这种区分大小写)时,认的答案就是匹配经典ASP行为,除非有一个很好的理由。

也就是说,我同意会议的案例不敏感性可以更好地记录在案。

BTW,ASP.NET会话集合从NameObjectCollectionBase获取其案例行为,它是Cookie,会话状态,应用程序状态,标题等的所有ASP.NET内置对象的基类。从文档:

The underlying structure for this
class is a hash table.

Each element is a key/value pair.

The capacity of a
NameObjectCollectionBase is the number
of elements the
NameObjectCollectionBase can hold. As
elements are added to a
NameObjectCollectionBase,the capacity
is automatically increased as required
through reallocation.

The hash code provider dispenses hash
codes for keys in the
NameObjectCollectionBase instance. The
default hash code provider is the
CaseInsensitiveHashCodeProvider.

The comparer determines whether two
keys are equal. The default comparer
is the CaseInsensitiveComparer.

In .NET Framework version 1.0,this
class uses culture-sensitive string
comparisons. However,in .NET
Framework version 1.1 and later,this
class uses
CultureInfo..::.InvariantCulture when
comparing strings. For more
information about how culture affects
comparisons and sorting,see Comparing
and Sorting Data for a Specific
Culture Comparing and Sorting Data for
a Specific Cultureand Performing
Culture-Insensitive String Operations.

一个合理的后续问题是:为什么经典的ASP设计具有不区分大小写的键?这样做的原因是,在1996年(或周围),ASP使用的主要语言是VBScript,所以有意义的是满足对VB开发人员的不区分大小写的期望。

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....