I cannot for the life of me figure this out. I have a web api controller with Get and Post methods. The Get method works fine with and without parameters, but the post breaks when I try to add a String parameter to it. Below is my code.
Route:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{id}",
defaults: new { id = UrlParameter.Optional }
);
Controller:
public class AuditController : ApiController
{
public String Post(String test)
{
return "Success : " + test;
}
public String Get(String test)
{
return "Success : " + test;
}
}
Request:
var request = WebRequest.Create("http://localhost:42652/Audit");
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
using (var writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write("test=TEST");
}
WebResponse webResponse = request.GetResponse();
I've tried many variations on the request, I feel like there's something simple I'm missing. Thanks for your help.
id
, but in your method you pass a parameter namedtest
. What shall it be? Also primitive type parameters are always tried to read from the Uri, so your post method won't work. I really suggest you to work through all the very nice ASP.Net Web API tutorials on the asp.net homepage! – JustAnotherUserYouMayKnow Jun 11 at 15:44Audit?test=value
, but not onAudit/value
. The answer you accepted sum this up good, but I still encourage you to really work through the tutorials! – JustAnotherUserYouMayKnow 2 days ago