11

早晨,

我需要从我的网络服务返回一条消息。下面是我的代码示例,我正在返回一个字符串。

[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/"/>

理想情况下,我想返回类似的东西

 {"success" : true, "message" : "***Message Here***"}

我相信一旦我明白了,如果需要,我将能够退回其他物品。它只是我需要解决的这个基础。

非常感谢所有帮助,在此先感谢:)

更新:刚刚发现这个...

 return "{Message:'hello world'}"

我需要类似的东西吗

 responseText = "{"success" : true, "message" : \"There has been an error. Message: " + ex.Message + "\"}"
4

5 回答 5

14

采用:

[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>
于 2012-07-12T08:10:39.413 回答
2

请使用您的网络方法的属性

   [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]

调用者将他的内容类型设置为 application/json 以使用 web 方法

于 2012-07-12T08:11:17.597 回答
0

试试这个:

[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;
    }
于 2014-09-22T09:35:04.440 回答
0

要删除服务响应中的 XML 标记,请参阅 StackOverflow 上的此答案:

ASP.NET WebService 正在用 XML 标记包装我的 JSON 响应

于 2014-11-17T13:50:12.910 回答
0

这是我对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());
    }
}
于 2018-04-22T21:13:13.583 回答