【问题标题】:How to return more than a string from a webservice?如何从 Web 服务返回多个字符串?
【发布时间】:2012-05-30 09:55:38
【问题描述】:

我已经使用 C# 和 MySQL 在 asp.net 中创建了 webservice。我想从此服务返回多个值。我正在使用以下代码:

[WebMethod]
public string RegisterUserViaFacebook(string fbid, string fbmailid,string devicetype)  
{
    string success = "Already Registered";
    string id="", name="";

    if (!ExistsFBID(fbid))
    {
        name = GenerateUserName();

        string password = generatePassword(10);

        string insertUser = "Insert into tbl_userinfo(UserName,Password,Facebook_ID,Facebook_EmailID,DeviceType) values";
        insertUser += "( '" + name + "' ,'" + password + "','" + fbid + "','" + fbmailid + "','" + devicetype + "' )";
        con = new MySqlConnection(conString);
        con.Open();
        MySqlCommand cmd = new MySqlCommand(insertUser, con);
        success = cmd.ExecuteNonQuery().ToString();
        con.Close();

        string getID = "SELECT UserID from tbl_userinfo where UserName='" + name + "'  ";
        con = new MySqlConnection(conString);
        con.Open();
        MySqlCommand cmd1 = new MySqlCommand(getID, con);
        id = cmd1.ExecuteScalar().ToString();
        con.Close();


        if (Convert.ToInt16(success) > 0)
        {
            SendMail(fbmailid, name, password);
            success = "New User"  ;
        }
        else
            success = "Error in Insertion";
    }
    else
    {
        string getID1 = "SELECT UserID, UserName from tbl_userinfo where Facebook_ID='" + fbid + "' ";
        con = new MySqlConnection(conString);
        con.Open();
        MySqlCommand cmd2 = new MySqlCommand(getID1, con);
        MySqlDataReader info = cmd2.ExecuteReader();

        while (info.Read())
        {
            id = info.GetString(0);
            name = info.GetString(1);

        }
        con.Close();
    }

    string jsonString = JsonConvert.SerializeObject(success);
    String finalString = "{\"USER IS\":";
    finalString += jsonString;
    finalString += "}";

    string jsonString1 = JsonConvert.SerializeObject(id);
    String finalString1 =finalString + "{\"ID IS\":";
    finalString1 += jsonString1;
    finalString1 += "}" ;

    string jsonString2 = JsonConvert.SerializeObject(name);
    String finalString2 = finalString1 + "{\"NAME IS\":";
    finalString2 += jsonString2;
    finalString2 += "}";
    return finalString2;


 }

但它返回单个字符串中的所有值。我想将这些值分别返回为SucceessIDName

我该怎么做?

【问题讨论】:

  • 为什么不将值作为 array 返回??
  • @huMptyduMpty - 感谢您的回复。你能指导一下怎么做吗?
  • 为什么要手动编写 SQL 和 JSON?有大量的库可以用更少的代码和更少的错误可能性做同样的事情。
  • 什么在消耗你的网络服务?是否需要返回Json?
  • @Liam - 是的,它需要返回 Json

标签: c# asp.net mysql web-services


【解决方案1】:

创建一个简单的类来保存成功、id 和名称并返回它的序列化实例。

public class RegistrationResult
{
   public string Success { get; set; }
   public string Name { get; set; }
   public int Id { get; set; }
}

你可以这样做:

var result = new RegistrationResult { Success = success, Name = name, Id = id} ;
return JsonConvert.SerializeObject(success);

【讨论】:

  • 如果你不想创建一个类,你可以使用 out 参数。但我更喜欢上课。仅供参考
  • @geon - 但是 to 首先要序列化为 JSON,您需要一个适当的对象来保存所有这些值。
  • @Oded - 感谢您的回复。但是我该怎么做呢?我不知道要使用它。请帮帮我。
  • @Oded - 我使用了您的代码,但显示成功、名称和 ID 由于保护级别而无法访问
  • @RKP - 然后将它们公开。我更新了我的答案,以防你不明白这意味着什么。
【解决方案2】:

您可能希望在 JSON 中返回单个对象。

return "{user: "+JsonConvert.SerializeObject(success)+", id: "+JsonConvert.SerializeObject(id)+", name: "+JsonConvert.SerializeObject(name)+"}";

Oded 的回答也很好。说的清楚一点,不过还需要多加一个类。

【讨论】:

    猜你喜欢
    • 2014-08-24
    • 1970-01-01
    • 2019-04-03
    • 1970-01-01
    • 2012-02-23
    • 1970-01-01
    • 1970-01-01
    • 2014-11-15
    • 1970-01-01
    相关资源
    最近更新 更多