I have created a controller in the ASP.NET Web API. Below is the code for the controller
public class CalculateController : ApiController
{
public IEnumerable<string> Get()
{
return new string[] { "from get method" };
}
public IEnumerable<string> Post([FromBody]string value)
{
return new string[] { "from post method" };
}
}
Below is the code that I am using to make a post request to the webAPI
HttpWebRequest httpWReq = (HttpWebRequest)WebRequest.Create("http://localhost:62479/api/calculate");
StringBuilder postdata = new StringBuilder("value=Anshuman");
byte[] data = Encoding.UTF8.GetBytes(postdata.ToString());
httpWReq.Method = "POST";
httpWReq.ContentType = "application/x-www-form-urlencoded";
httpWReq.ContentLength = data.Length;
using (Stream stream = httpWReq.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
The problem is that even if I make a POST request even then the data is returned from the GET method of the Controller. I have not made any changes in the default configuration of the Web API project. I am using MVC 4.
Thanks for your time. If any other information is required then please add a comment.
I have both the projects running in Visual Studio 2012 on the same machine.