How can I return multiple parameters of string
type using web method (web service) in asp.net C#, here is my current code
[System.Web.Services.WebMethod]
public static string GetDetail(string oid, Label label)
{
string s = "select amount,amount_type from money where id='" +oid + "'";
connect con = new connect();
DataSet ds = con.get(s);
if (ds.Tables.Count > 0)
{
if (ds.Tables[0].Rows.Count > 0)
{
label.Text = ds.Tables[0].Rows[0]["a_type"].ToString();
string a=Math.Round(Convert.ToDecimal(ds.Tables[0].Rows[0][0]), 2).ToString();
return a + " " + label;
}
else
{
return "no";
}
}
else
{
return "no";
}
}
The recommended method is to create custom data type class and return data as the class type instead of string.
Like this suppose you have this class
[Serializable]
public sealed class MyDataType
{
public string Data{ get; set; }
public string Data1 { get; set; }
}
then your web method in web service should be like
[WebMethod(EnableSession = true)]
public MyDataType TestMethod(string testId)
{
//Some Code
string data = string.Empty;
string data1 = string.Empty;
List<test1> datalist1 = new List<test1>();
List<test> datalist = new List<test>();
//coding
data = jsonSerialize.Serialize(datalist1);
data1 = jsonSerialize.Serialize(datalist);
return new MyData { Data = data, Data1 = data1 };
}
Another method, not a good way but you could return an array of strings:
public string[] WebServiceMethod(...)
{
return new string[] { data, data1 };
}
You'd then need to perform each bit of JSON deserialization separately on the client, which isn't a pleasant task
Or third solution is to check this link
You can simply return multiple values in web-api in C# as below
[HttpGet]
[Route("api/getexample")]
public IHttpActionResult getAll()
{
//some code here
return Ok(new { data1List, data2List, someValue });
}
That should work.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly