Asp.net web api WebAPI:带有查询字符串的PostAsync无效

Asp.net web api WebAPI:带有查询字符串的PostAsync无效,asp.net-web-api,Asp.net Web Api,请参阅我的示例web api操作,它采用一个字符串类型参数 [RoutePrefix("api/customer")] public class CustomerController : ApiController { [HttpPost, Route("DeleteCustomer")] public HttpResponseMessage DeleteProduct(string customerID) { HttpR

请参阅我的示例web api操作,它采用一个字符串类型参数

[RoutePrefix("api/customer")]
public class CustomerController : ApiController
{

        [HttpPost, Route("DeleteCustomer")]
        public HttpResponseMessage DeleteProduct(string customerID)
        {
            HttpResponseMessage response = null;
            Customer customer = repository.Get(customerID);
            if (customer == null)
            {
                var message = string.Format("No customer found by the ID {0}", customerID);
                HttpError err = new HttpError(message);
                response = Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, err);
                response.ReasonPhrase = message;
            }
            else
            {
                if(repository.Remove(customerID))
                {
                    response = Request.CreateResponse<Customer>(HttpStatusCode.Created, customer);
                    response.ReasonPhrase = "Customer successfully deleted";
                }
                else
                {
                    var message = string.Format("Due to some error customer not removed");
                    HttpError err = new HttpError(message);
                    response = Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, err);
                    response.ReasonPhrase = message;
                }
            }


            return response;
        }

}
[RoutePrefix(“api/客户”)]
公共类CustomerController:ApiController
{
[HttpPost,路线(“删除客户”)]
公共HttpResponseMessageDeleteProduct(字符串customerID)
{
HttpResponseMessage响应=null;
Customer=repository.Get(customerID);
如果(客户==null)
{
var message=string.Format(“ID{0}未找到客户”,customerID);
HttpError err=新的HttpError(消息);
response=Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed,err);
response.ReasonPhrase=消息;
}
其他的
{
if(repository.Remove(customerID))
{
response=Request.CreateResponse(HttpStatusCode.Created,客户);
response.ReasonPhrase=“客户已成功删除”;
}
其他的
{
var message=string.Format(“由于某些错误,客户未被删除”);
HttpError err=新的HttpError(消息);
response=Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed,err);
response.ReasonPhrase=消息;
}
}
返回响应;
}
}
下面用http客户端这样调用,但不工作,并给出错误找不到

private void btnDelete_Click(object sender, EventArgs e)
{
    var uri = new Uri(ConfigurationManager.AppSettings["baseAddress"] + "/api/customer/DeleteCustomer");

    var content = new FormUrlEncodedContent(new[] 
    {
        new KeyValuePair<string, string>("customerID", "CUS01")
    });

    try
    {
        using (var client = new HttpClient())
        {
            using (var response = client.PostAsync(uri, content).Result)
            {
                if (response.IsSuccessStatusCode)
                {
                    MessageBox.Show(response.ReasonPhrase);
                }
                else
                {
                    Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
                    var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(response.Content.ReadAsStringAsync().Result);
                    MessageBox.Show(dict["Message"]);
                }
            }
        }
    }
    catch (HttpRequestException ex)
    {
        // catch any exception here
    }            

}
private void btnDelete\u单击(对象发送者,事件参数e)
{
var uri=new uri(ConfigurationManager.AppSettings[“baseAddress”]+“/api/customer/DeleteCustomer”);
var content=newformurlencodedcontent(new[]
{
新的KeyValuePair(“customerID”、“CUS01”)
});
尝试
{
使用(var client=new HttpClient())
{
使用(var response=client.PostAsync(uri,content.Result)
{
if(响应。IsSuccessStatusCode)
{
MessageBox.Show(response.ReasonPhrase);
}
其他的
{
Console.WriteLine(“{0}({1})”,(int)response.StatusCode,response.ReasonPhrase);
var dict=JsonConvert.DeserializeObject(response.Content.ReadAsStringAsync().Result);
MessageBox.Show(dict[“Message”]);
}
}
}
}
捕获(HttpRequestException-ex)
{
//这里有什么例外吗
}            
}
有人请看一下我的代码,告诉我在调用代码时哪里出错了?谢谢

[RoutePrefix(“api/客户”)] 公共类CustomerController:ApiController {

[HttpPost,路由(“删除客户”)]
公共HttpResponseMessageDeleteProduct([FromBody]字符串customerID)
{
HttpResponseMessage响应=null;
Customer=repository.Get(customerID);
如果(客户==null)
{
var message=string.Format(“ID{0}未找到客户”,customerID);
HttpError err=新的HttpError(消息);
response=Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed,err);
response.ReasonPhrase=消息;
}
其他的
{
if(repository.Remove(customerID))
{
response=Request.CreateResponse(HttpStatusCode.Created,客户);
response.ReasonPhrase=“客户已成功删除”;
}
其他的
{
var message=string.Format(“由于某些错误,客户未被删除”);
HttpError err=新的HttpError(消息);
response=Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed,err);
response.ReasonPhrase=消息;
}
}
返回响应;
}
}

能否在方法参数中添加[FromBody]关键字

[RoutePrefix(“api/客户”)] 公共类CustomerController:ApiController {

[HttpPost,路由(“删除客户”)]
公共HttpResponseMessageDeleteProduct([FromBody]字符串customerID)
{
HttpResponseMessage响应=null;
Customer=repository.Get(customerID);
如果(客户==null)
{
var message=string.Format(“ID{0}未找到客户”,customerID);
HttpError err=新的HttpError(消息);
response=Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed,err);
response.ReasonPhrase=消息;
}
其他的
{
if(repository.Remove(customerID))
{
response=Request.CreateResponse(HttpStatusCode.Created,客户);
response.ReasonPhrase=“客户已成功删除”;
}
其他的
{
var message=string.Format(“由于某些错误,客户未被删除”);
HttpError err=新的HttpError(消息);
response=Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed,err);
response.ReasonPhrase=消息;
}
}
返回响应;
}
}


能否在方法参数中添加[FromBody]关键字

请更具体地说明错误发生的位置以及“未找到”响应的内容。IsSuccessStatusCode不返回成功并且未调用web api操作。请更具体地说明错误发生的位置以及“未找到”的内容response.IsSuccessStatusCode未返回成功,且未调用web api操作。如果我的调用不正确,请添加调用代码。请先看一下我的电话号码,告诉我是这样吗?我不知道你为什么要打PostAsync?你能试试client.Post()吗?如果我的调用
    [HttpPost, Route("DeleteCustomer")]
    public HttpResponseMessage DeleteProduct([FromBody]string customerID)
    {
        HttpResponseMessage response = null;
        Customer customer = repository.Get(customerID);
        if (customer == null)
        {
            var message = string.Format("No customer found by the ID {0}", customerID);
            HttpError err = new HttpError(message);
            response = Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, err);
            response.ReasonPhrase = message;
        }
        else
        {
            if(repository.Remove(customerID))
            {
                response = Request.CreateResponse<Customer>(HttpStatusCode.Created, customer);
                response.ReasonPhrase = "Customer successfully deleted";
            }
            else
            {
                var message = string.Format("Due to some error customer not removed");
                HttpError err = new HttpError(message);
                response = Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, err);
                response.ReasonPhrase = message;
            }
        }


        return response;
    }