.NET HttpSessionState不区分大小写 [英] .NET HttpSessionState Case Insensitivity

查看:347
本文介绍了.NET HttpSessionState不区分大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.NET的 HttpSessionState 使用是InProc 店似乎把会话变量键值不区分大小写。例如:

.NET's HttpSessionState using an "InProc" store seems to treat session variable key values as case insensitive. For example:

session["foo"] = 1;
session["Foo"] = 2;
Trace.Write(session["foo"].ToString()); // => 2

此行​​为似乎是无证的,所以我想知道,如果是单纯的基本会话存储机制的副作用,或者通过类本身故意实施。 由于C#把一切作为区分大小写的,这是一个有点令人不安的会议上不采取行动以同样的方式。怎么办?是否按存储类型有什么不同?它是为了与VB向后兼容?

This behavior appears to be undocumented, so I'm wondering if it is simply a side-effect of the underlying session store mechanism, or intentionally implemented by the class itself. Since C# treats everything else as case-sensitive, it is a bit unnerving for the session to not act the same way. What gives? Does it differ by store type? Is it there for backwards-compatibility with VB?

推荐答案

HttpSessionState的键作了不区分大小写相匹配的经典ASP会话对象的行为。这使得开发人员更容易将他们的ASP应用到ASP.NET不引入微妙的区分大小写的问题。

HttpSessionState's keys were made case-insensitive to match the behavior of the classic ASP Session object. This made it easier for developers to port their ASP applications to ASP.NET without introducing subtle case-sensitivity issues.

同样不区分大小写键的行为是查询字符串,饼干等物品和真实其他类似的对经典的ASP内建对象。

The same case-insensitive-key behavior is true of the QueryString, Cookies, etc. objects and other similar-to-Classic-ASP built-in objects.

我是在微软的IIS团队在ASP.NET正在设计,和ASP.NET努力保持ASP.NET code向后兼容ASP尽可能。这并不意味着ASP.NET有完美的向后兼容性,但每当一个决定,来到了(这样的区分大小写的),默认的答案是,以配合经典ASP行为,除非有很好的理由不这样做。

I was on the IIS team at Microsoft when ASP.NET was being designed, and the ASP.NET worked hard to keep ASP.NET code backwards compatible to ASP wherever possible. That doesn't mean ASP.NET had perfect backwards compatibilty, but whenever a decision came up (like this case-sensitivity one), the default answer was to match Classic ASP behavior unless there was a good reason not to.

这是说,我同意,会议的不区分大小写,可以更好地记录在案。

That said, I agree that Session's case-insensitivity could be better documented.

顺便说一句,ASP.NET会话集合会从<一的情况下的行为href="http://msdn.microsoft.com/en-us/library/system.collections.specialized.nameobjectcollectionbase.aspx">NameObjectCollectionBase这是所有内置的饼干,会话状态,应用程序状态,标题等对象ASP.NET的基类从文档:

BTW, the ASP.NET Session collection gets its case behavior from NameObjectCollectionBase which is the base class for all the ASP.NET built-in objects for Cookies, Session State, Application State, Headers, etc. From the docs:

的基础结构此   类是一个哈希表。

The underlying structure for this class is a hash table.

每个元素都是一个键/值对。

Each element is a key/value pair.

的能力   NameObjectCollectionBase是数   元素的   NameObjectCollectionBase可以持有。如   元素添加到一   NameObjectCollectionBase,容量   自动增加的要求   通过重新分配。

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.

散列code供应商免除哈希   codeS在钥匙   NameObjectCollectionBase实例。该   缺省散列code提供者是   CaseInsensitiveHash codeProvider。

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

比较器确定是否将两个   密钥是相同的。默认的比较   是CaseInsensitiveComparer。

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

在.NET Framework 1.0版,这   类使用的文化敏感字符串   比较。然而,在.NET   Framework 1.1版及更高版本,这   类用途   的CultureInfo .. ::。InvariantCulture的时候   比较字符串。欲了解更多   有关如何文化影响的信息   比较和排序,请参见比较   和排序数据的特定   文化比较和排序数据   特定的文化现象表演   不区分区域性的字符串操作。

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开发人员不区分大小写的期望。

A reasonable follow-up question would be: why was classic ASP designed with case-insensitive keys? The reason for this is that, back in 1996 (or thereabouts) the main language used with ASP was VBScript, so it made sense to cater to the case-insensitive expectations of VB developers.

这篇关于.NET HttpSessionState不区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆