0

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.

enter image description here

mbigun
  • 1,304
  • 4
  • 19
  • 46

2 Answers2

1

have you enabled the "delete" verb in IISExpress

http://www.iis.net/learn/extensions/introduction-to-iis-express/iis-express-faq http://stevemichelotti.com/resolve-404-in-iis-express-for-put-and-delete-verbs/

You can also manually test PUT,DELET verbs with Fiddler

Steve
  • 1,995
  • 2
  • 16
  • 25
  • Thanks! Links are very useful. I checked PUT and DELET verbs - both are enabled in IISExpress. – mbigun Oct 02 '15 at 21:31
  • you can use the compose tab in fiddler to test the DELETE verb http://www.intstrings.com/ramivemula/articles/testing-asp-net-web-apiget-post-put-delete-using-fiddler/ – Steve Oct 03 '15 at 04:10
0

Issue has been resolved. I have tried almost everething I found related to this problem. Based on an error code, I was 100% sure it is a bad request/url I'm sending to the server.
Once I changed server behavior in the config file to see if any exceptions are on a server side I found what causes the problem.

  <serviceBehaviors>
    <behavior name="Default">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>

I turned includeExceptionDetailInFaults to true and got following details:

enter image description here

Based on a description, my server couldn't process request due to wrong LINQ query. Implementation of my DELETE method is following:

public bool DeleteNews(string id)
{
    using (EF.ServiceDBEntities context = new EF.ServiceDBEntities())
    {
        var n = context.News.FirstOrDefault(x => x.NewsID == int.Parse(id));
        if (n != null)
        {
            context.News.Remove(n);
            context.SaveChanges();
            return true;
        }
    }
    return false;
}

Such condition x => x.NewsID == int.Parse(id) is not allowed. When I moved int.Parse(id) out of the query issue has dissapeared. Some details here.
Even with this, I can't understand why my server returned erorr code 400 (bad request)? This exception is produced by a server, isn't it? As I know there is code 500 which belongs to the internal server errors and my exception is a good sample of it. Any comments are appreciated.

Community
  • 1
  • 1
mbigun
  • 1,304
  • 4
  • 19
  • 46