I checked ton of posts/tutorials/videos but still can't get working code with a DELETE method in one specific case: when I'm trying to DELETE by ID.
Working sample below. I'm passing whole json string to DELETE method and it works perfectly (based on it I dare to suggest that there is no errors in the wcf/client configuration files).
WCF
[OperationContract]
[WebInvoke(Method = "DELETE",
ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json,
UriTemplate = "delete"
)]
bool DeleteNews(News news);
Client:
const string WEBSERVICE_URL = "http://localhost:33873/NewsService.svc/delete";
string jsonData = "{my json string}";
try
{
var webRequest = WebRequest.Create(WEBSERVICE_URL);
if (webRequest != null)
{
webRequest.Method = "DELETE";
webRequest.Timeout = 20000;
webRequest.ContentType = "application/json";
using (Stream s = webRequest.GetRequestStream())
{
using (StreamWriter sw = new StreamWriter(s))
sw.Write(jsonData);
}
using (Stream s = webRequest.GetResponse().GetResponseStream())
{
}
}
}
Now would like to demonstrate following code which doesn't work for me. I'm wondering that I can't use DELETE by ID.
WCF
[OperationContract]
[WebInvoke(Method = "DELETE",
UriTemplate = "delete/?id={id}"
)]
bool DeleteNews(int id);
Putting URL to the browser like http://localhost:33873/NewsService.svc/delete/?id=10 and getting "Remote server returned bad request with a code 400" (meaning something wrong on a client side or with my request). I have also tried via string parameter like below:
[OperationContract]
[WebInvoke(Method = "DELETE",
UriTemplate = "delete/{id}"
)]
bool DeleteNews(string id);
After such transformation, URL looks like http://localhost:33873/NewsService.svc/delete/10
Result is also unsuccessfull with the same error.