我们注意到,有时从
AJAX调用结果到控制器操作,JSON结果的情况是不正确的.如果我们重建我们的解决方案并尝试完全相同的调用,则返回的案例实际上会发生变化在下面的例子中,密钥的情况已经纠正了一年多,直到现在它已经决定根据一些看似随意的情况开始随机改变.
正如您在上图中所看到的,JSON结果的关键是小写“成功”.但是,当我在Chrome控制台中查看结果时,它是一个大写的“成功”.这导致我们的JavaScript失败,因为它正在检查小写版本.
是什么造成的?更重要的是,我们如何阻止这种情况?
vb.net不区分大小写,而不区分大小写的C#.这意味着编译器将为以下每个匿名类型生成一个类(来自第一个实例):
Dim a = New With {.success = True} 'Compiler generate a class based on this type Dim b = New With {.Success = True} 'Same type as `a` Dim c = New With {.sUcCeSs = True} 'Same type as `a` Debug.WriteLine(a.GetType().Name) Debug.WriteLine(b.GetType().Name) Debug.WriteLine(c.GetType().Name)
VB$AnonymousType_0’1
VB$AnonymousType_0’1
VB$AnonymousType_0’1
以下是编译回vb.net时编译代码的样子:
<DebuggerDisplay("success={success}"),CompilerGenerated> _ Friend NotInheritable Class VB$AnonymousType_0(Of T0) ' Methods <DebuggerNonUserCode> _ Public Sub New(ByVal success As T0) Me.$success = success End Sub <DebuggerNonUserCode> _ Public Overrides Function ToString() As String Dim builder As New StringBuilder builder.Append("{ ") builder.AppendFormat("{0} = {1} ","success",Me.$success) builder.Append("}") Return builder.ToString End Function Public Property success As T0 <DebuggerNonUserCode> _ Get Return Me.$success End Get <DebuggerNonUserCode> _ Set(ByVal Value As T0) Me.$success = Value End Set End Property Private $success As T0 End Class