Please help me! I am experiencing an odd problem with anonymous types in Asp.net/WebForms using Visual Studio 2008.
In the page markup, this generates a “Type Expected” error (just an example):
<%=new { property = "somevalue" }.ToString() %>
Or even
<%var x = new { property = "somevalue" };
Response.Write(x.ToString()); %>
If I put this in a method, this works fine:
<%=ShowIt() %>
…in codebehind…
public string ShowIt()
{ return new { property = "somevalue" }.ToString(); }
What can I do to get Web Forms to recognize anonymous type/syntax?
I tried Google.
PLEASE NOTE: I am aware that the above code is not good practice. I can’t seem to use anonymous types AT ALL in markup in this project. IE: even passing as an argument:
<%=ShowIt(new { prop1 = "a", prop2 = 2 }) %>
Does not seem to work.
I discovered the problem in the web.config. Apparently it was missing a reference to the C# compiler under the “compilers” tag which is under the “system.codedom” tag.