ASP.Net WebService中的匿名返回

时间:2014-02-28 09:25:39

标签: c# asp.net json web-services asmx

我创建了一个webservice函数,它返回一个对象(匿名linq结果)

    [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;
    }

如果我使用Fiddler来获得响应,它会返回ok我想要的数据

enter image description here

但如果我使用浏览器,则只会返回错误

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()

问题:如何将匿名方法的结构+数据显示到json中,这将在浏览器中显示如下

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

或默认的XML结果。

提前致谢

1 个答案:

答案 0 :(得分:2)

错误是Content-Type标头。尝试将其从Fiddler中的请求中删除,您将看到相同的异常。浏览器不发送Content-Type: application/json它,您的webserice尝试返回XML序列化对象并失败。这通常是ASP.NET安全策略。有关详细信息,请查看此url