How to create API in Asp.net MVC parameters separeted with '&' symbole ?
like :
http://localhost:22332/api/authorize?user=ury&session=token&item=an_item http://localhost:22332/api/authorize?user=ury&session=token&item=an_item Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Bhuvanesh MohankumarPosted May 13, 2016, 5:08 AM
Yes this is possible, that to have a query stringtype values to be passed to the web api service.
please lookat this example and you can understand the same.
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; namespace Thrita.Web.Api.FreeWebApi.Controllers { public class ValuesController : ApiController { // GET api/values public IEnumerable Get()
{
var allUrlKeyValues = ControllerContext.Request.GetQueryNameValuePairs();
string p1Val = allUrlKeyValues.SingleOrDefault(x => x.Key == "p1").Value;
string p2Val = allUrlKeyValues.SingleOrDefault(x => x.Key == "p2").Value;
string p3Val = allUrlKeyValues.SingleOrDefault(x => x.Key == "p3").Value;
return new string[] { "value1", "value2" , p1Val, p2Val, p3Val };
}
// GET api/values/5
public string Get(int id)
{
return "value";
}
// POST api/values
public void Post([FromBody]string value)
{
}
// PUT api/values/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/values/5
public void Delete(int id)
{
}
}
}
Mark as answer, if helpful.