Reputation: 100381
I need a Hello World
example...
[WebService(Namespace = "xxxxx")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService()]
public class Something : System.Web.Services.WebService
{
public Something() { }
[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public string HelloWorld()
{
return "{Message:'hello world'}";
}
}
Because it generates an error
{"Message":"Invalid JSON primitive: value.","StackTrace":" at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String input)\r\n at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context, JavaScriptSerializer serializer)\r\n at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.ArgumentException"}
What's wrong?
Edit: And what if I need to return {Message:'',Type:1}
?
Edit2: The answer for the last one is: I can return a Dictionary<string, string>
Upvotes: 2
Views: 1561
Reputation: 28434
Well, if you need your JSON to be exactelly in the {Message:Hello wolrd} pair (or if you plan to use your webservice for anything more complex) you will probably need to create a serializable class for that
[Serializable]
class MessageClass
{
public string Message {get;set;}
//any public member will automatically be serialized as JSON
punlic bool WillAlsoBeJsoned {get;set;}
//unless explicitly told not to
[NonSerialized]
[ScriptIgnoreAttribute]
public string WillNotBeJsoned {get;set;}
}
then in your webservice
//{Message :"Hello World",WillAlsoBeJsoned :false}
[WebMethod]
public string HelloWorld()
{
var returnObject = new MessageClass();
returnObject.Message= "Hello World";
var oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
return oSerializer.Serialize(returnObject);
}
// return an array of the same items
//[{Message :"Hello World",WillAlsoBeJsoned :false},
//{Message :"something else",WillAlsoBeJsoned :false}]
[WebMethod]
public string HelloWorldList()
{
var returnObject = new List<MessageClass>();
var message1= new MessageClass();
message1.Message= "Hello World";
var message2= new MessageClass();
message2.Message= "something else";
returnObject.Add(message1);
returnObject.Add(message2);
var oSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
return oSerializer.Serialize(returnObject);
}
Upvotes: 1
Reputation: 2417
You only need to return a normal (that is, non-JSON) String from your function like:
return "Hello World"
Assuming you are using .Net 3.5, your JSON response will be:
{"d":"Hello World"}
Upvotes: 5