Anonymous type VB.net has an incorrect property body from an AJAX call

We noticed that sometimes from the results of an AJAX call to the controller action, the JSON result is incorrect. The returned case will really change if we rebuild our solution and try the same challenge. In the following case, the key case was correct during the year and until now, when he decided to start a random change depending on some seemingly random circumstances.

Casing issue

As you can see in the picture above, the key to the JSON result is lowercase success. However, when I look at the results in the Chrome console, this is an uppercase “Success”. This causes our JavaScript to crash as it checks the lowercase version.

What causes this? And more importantly, how to stop it?

+7
json ajax
source share
1 answer

vb.net is case insensitive, unlike case sensitive C# . This means that the compiler will only generate one class (from the first instance) for each of the following anonymous types:

 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

Here's what the executed code looks like when compiling back to 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 
+5
source share

All Articles