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
Anonymous return in ASP.Net WebService
-
18-10-2022 - |
Question
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
but if i use the browser, it just return an error
System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: <>f__AnonymousType2`2[System.Collections.Generic.List`1[<>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
No correct solution
OTHER TIPS
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow