I have a controller where I am trying to post the following with a query string
http://localhost:53546/api/v1/projects?id=ABA28A61-8898-4739-8464-386C7890E435
but that doesn't hit the controller POST
method
This however does
http://localhost:53546/api/v1/projects/ABA28A61-8898-4739-8464-386C7890E435
How can I accept a query string in a post? I keep getting a 504 error : Method not supported. Is there some special syntax to accept query strings in asp.net web api?
Here is my controller
[RoutePrefix("api/v1/projects")]
public class ProjectController : ApiController
{
private RestClient client;
public ProjectController() {
client = new RestClient("http://localhost:53546");
}
[HttpPost]
[Route("{id:guid}")]
public string Post(Guid id)
{
return "Here is your Post id - " + id.ToString();
}
}
Here is my DoPost
method
public JToken DoRequest(string path, string method, params string[] parameters)
{
if (!path.StartsWith("/"))
{
path = "/" + path;
}
var fullUrl = url + path + ToQueryString(parameters);
if (DebugUrls) Console.WriteLine("Requesting: {0}", fullUrl);
var request = WebRequest.Create(new Uri(fullUrl));
request.Method = method;
request.ContentType = "application/json";
request.ContentLength = 0;
var response = request.GetResponseAsync().Result;
using (var responseStream = response.GetResponseStream())
{
return ReadResponse(responseStream);
}
}
This is my route config if it matters
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
As previously mentioned you have defined id as part of your default route. This means that when an API controller has a method accepting id as an argument, the routing engine is going to expect to find it in the uri and not from a query string.
In your POST example above, if you changed the argument name from id to postId and changed id to postId in your DoRequest code then it should work.
Another option would be to remove the {id} from your routeTemplate and remove the default value for id as well.