1

I created a webservice function which returns an object (anonymous linq result)

 [WebMethod]
 [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
 public object GetUser()
 {
 List<string> users = new List<string>
 {
 "Nora Aunor",
 "Pilita Corrales"
 };
 var result = users.Select(u => new
 {
 Name = u,
 Birthdate = DateTime.Now
 })
 .ToList();
 var retVal = new
 {
 Data = result,
 Count = result.Count
 };
 return retVal;
 }

if i use Fiddler to get the response, it returns ok with what data i expected

enter image description here

but if i use the browser, it just return an error

System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: &lt;&gt;f__AnonymousType2`2[System.Collections.Generic.List`1[&lt;&gt;f__AnonymousType1`2[System.String,System.DateTime]],System.Int32] cannot be serialized because it does not have a parameterless constructor.
 at System.Xml.Serialization.TypeDesc.CheckSupported()
 at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError)
 at System.Xml.Serialization.XmlSerializationWriter.CreateUnknownTypeException(Type type)
 at System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(String name, String ns, Object o, Boolean xsiType)
 at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write1_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType)
 at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write3_anyType(Object o)
 at Microsoft.Xml.Serialization.GeneratedAssembly.ObjectSerializer1.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
 at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
 --- End of inner exception stack trace ---
 at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
 at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces)
 at System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponse response, Stream outputStream, Object returnValue)
 at System.Web.Services.Protocols.HttpServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream)
 at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues)
 at System.Web.Services.Protocols.WebServiceHandler.Invoke()

Question: how to show the structure + data of the anonymous method into json that will show in the browser like this

{
 "d": {
 "Data": [{
 "Name": "Nora Aunor",
 "Birthdate": "\/Date(1393578983512)\/"
 },
 {
 "Name": "Pilita Corrales",
 "Birthdate": "\/Date(1393578983512)\/"
 }],
 "Count": 2
 }
}

or the default XML result.

Thanks in advance

Brian Rogers
130k31 gold badges315 silver badges315 bronze badges
asked Feb 28, 2014 at 9:25
2

1 Answer 1

2

Error is a Content-Type header. Try to remove it from request in Fiddler and you'll see same exception. Browser doesn't send Content-Type: application/json it and your webserice tries to return XML serialized object and fails. This is generally ASP.NET security policy. Check this url for more information

answered Feb 28, 2014 at 10:53
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.