.NET HttpSessionState案例不敏感

前端之家收集整理的这篇文章主要介绍了.NET HttpSessionState案例不敏感前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用“InProc”存储的.NET的HttpSessionState似乎将会话变量键值视为不区分大小写。例如:
session["foo"] = 1;
session["Foo"] = 2;
Trace.Write(session["foo"].ToString()); // => 2

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

解决方法

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

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

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

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

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

The underlying structure for this
class is a hash table.@H_403_6@

Each element is a key/value pair.@H_403_6@

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.@H_403_6@

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

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

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.@H_403_6@

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

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