C# 如何从webservice返回JSON
早上好 我需要从我的web服务返回一条消息。下面是我的代码示例,我返回一个字符串C# 如何从webservice返回JSON,c#,asp.net,json,web-services,C#,Asp.net,Json,Web Services,早上好 我需要从我的web服务返回一条消息。下面是我的代码示例,我返回一个字符串 [web method] public string CheckFeedSubmission() { string responseText = ""; try { //Stuff goes here responseText = "It Worked!" } catch (Exc
[web method]
public string CheckFeedSubmission()
{
string responseText = "";
try
{
//Stuff goes here
responseText = "It Worked!"
}
catch (Exception ex) { responseText = "Opps wehave an error! Exception message:" + ex.Message; }
return responseText ;
}
我目前得到以下回应
<string xmlns="http://tempuri.org/"/>
我相信,一旦我有了这个想法,如果需要的话,我将能够归还其他物品。这就是我需要解决的问题
非常感谢所有帮助,提前感谢:)
更新:刚找到这个
return "{Message:'hello world'}"
我需要像这样的东西吗
responseText = "{"success" : true, "message" : \"There has been an error. Message: " + ex.Message + "\"}"
使用:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]//Specify return format.
public string CheckFeedSubmission()
{
string responseText = "";
try
{
//Stuff goes here
responseText = "It Worked!"
}
catch (Exception ex) { responseText = "Opps wehave an error! Exception message:" + ex.Message; }
return responseText ;
}
<string xmlns="http://tempuri.org/"/>
{"success" : true, "message" : "***Message Here***"}
</string>
返回的结果如下:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]//Specify return format.
public string CheckFeedSubmission()
{
string responseText = "";
try
{
//Stuff goes here
responseText = "It Worked!"
}
catch (Exception ex) { responseText = "Opps wehave an error! Exception message:" + ex.Message; }
return responseText ;
}
<string xmlns="http://tempuri.org/"/>
{"success" : true, "message" : "***Message Here***"}
</string>
{“success”:true,“message”:“***message Here***”}
请使用webmethod的属性
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
调用者将contenttype设置为application/json以使用webmethod,请尝试以下方法:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]//Specify return format.
public bool addUser(UserModel um)
{
bool result = false;
result = Conversion.intToBool(SplashAwardsDB.executeNonQuery(
"INSERT INTO dbo.User ("
+ "userName, password, firstName, lastName, address, contactNo, birthDate, familyID, familyRole, x, y ) "
+ " VALUES ("
+ "'" + um.userName + "', "
+ "'" + um.password + "', "
+ "'" + um.firstName + "', "
+ "'" + um.lastName + "', "
+ "'" + um.address + "', "
+ "'" + um.contactNo + "', "
+ "'" + um.birthDate + "', "
+ "'" + um.familyID + "', "
+ "'" + um.familyRole + "', "
+ "'" + um.x + "', "
+ "'" + um.y + "')"
));
return result;
}
要删除服务响应中的XML标记,请参阅StackOverflow上的以下回答:
这是我针对framewok 4.5.2的解决方案, 在类FilterConfig中添加以下代码, 注意:您将需要lib Newtonsoft
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Serialize;
GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
GlobalConfiguration.Configuration.EnableCors();
filters.Add(new HandleErrorAttribute());
}
}
不起作用,仍然只返回一个字符串-谢谢,但如上所述,我仍然返回XML字符串实际上它在XML中返回Json。您需要在通话中指定要返回的内容。为什么它以这种格式返回?重新看一看答案。是的,你可以,看一看,我明白了,只是在我的应用程序中测试过,而不是在我的实际web服务及其返回的json中测试过。只需要格式化它,这样我就有了我想要返回的内容:)如果我像我在responseText中那样添加bool来传递真/假,我会正确假设吗?可能是