Niklas
Niklas

Reputation: 1853

Cant get param from delete method in web API

I have a web api server and want to implement a delete function:

this is the function:

    public void Delete([FromBody]string identifier)
    {
        ExpressLogger.LogDebug("Enter");
        try
        {


            if (Utils.IsRmhUsed())
            {

                CustomerRmhDbRepository repo = new CustomerRmhDbRepository();
                repo.DelCustomer(identifier);
            }
            else
            {
                //If implement function for RMS
            }
        }
        catch (Exception ex)
        {
            ExpressLogger.LogError(ex, ex.Message);
            HttpResponseMessage message = new 
            HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new S 
            tringContent(ex.Message) };
            throw new HttpResponseException(message);
        }
    }

I call the method from postman with the following string http://10.0.0.129:5005/api/customer?identifier=000007

The call gets to the delete method but the variable identifer is NULL I have tried like this to: http://10.0.0.129:5005/api/customer?000007

I also tried to use attribute like this:

[Route("{identifier}")]

When i do that it wont even 'go in the the method" Delete.

What can be wrong? Why cant i get the value from the variable identifier ?

Upvotes: 1

Views: 172

Answers (1)

Nkosi
Nkosi

Reputation: 247471

Remove the [FromBody] attribute in order to match

 http://10.0.0.129:5005/api/customer?identifier=000007

HTTP DELETE requests do not have a BODY and the model binder will bind the parameter from the query string.

If using the route template

[HttpDelete]
[Route("{identifier}")]
public void Delete(string identifier) {
    //...
}

The URL will need to look like

http://10.0.0.129:5005/api/customer/000007

in order to match the route template

Upvotes: 3

Related Questions