Reputation: 7170
This is my first web service, written using .NET 4.6.
I'm using System.Web.Services.WebService
.
How can I pass parameter in query string to a web service ?
If I call the URL: http://localhost:11111/myWebService.asmx/GetWorldById
and try to pass also parameters like
http://localhost:111/myWebService.asmx/GetWorldById?worldid=1
I got error: Request format is unrecognized for URL unexpectedly ending in '/GetWorldById'
This is my code
[WebMethod]
public string GetWorldById(int worldid)
{
using (MySqlConnection db = new MySqlConnection(connString))
{
MySqlCommand cmd = new MySqlCommand("", db);
MySqlDataReader dr;
cmd.CommandText = "SELECT * FROM myTable WHERE worldid='" + param1+ "'";
db.Open();
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
PopulateListFromDataReader(dr);
}
}
JavaScriptSerializer js = new JavaScriptSerializer();
return (js.Serialize(worldsList));
}
Upvotes: 0
Views: 2303
Reputation: 392
you can always read parameters from the requests
int worldid = int.Parse(HttpContext.Current.Request[nameof(worldid)]);
but you need a post request anyway.
Upvotes: 1