小编Dha*_*nki的帖子

使用HttpClient.PostAsJsonAsync发布标量数据类型

我正在使用HttpClient调用ASP .Net Web API并成功调用操作.此外,我也可以将自定义对象发布到操作中.

现在我面临的问题是,无法发布标量数据类型,如Integer,String等...

下面是我的控制器和调用操作的应用程序代码

//测试调用的应用程序

[Test]
        public void RemoveCategory()
        {
            HttpClient client = new HttpClient();

            HttpRequestMessage request = new HttpRequestMessage();

            HttpResponseMessage response = client.PostAsJsonAsync<string>("http://localhost:49931/api/Supplier/RemoveCategory/", "9").Result;

            Console.WriteLine(response.Content.ReadAsStringAsync().Result);
        }
Run Code Online (Sandbox Code Playgroud)

// Web API中的控制器和操作

public class SupplierController : ApiController
   {
    NorthwindEntities context = new NorthwindEntities();

    [HttpPost]
    public HttpResponseMessage RemoveCategory(string CategoryID)
    {
    try
    {
    int CatId= Convert.ToInt32(CategoryID);
    var category = context.Categories.Where(c => c.CategoryID == CatId).FirstOrDefault();
    if (category != null)
    {
    context.Categories.DeleteObject(category);
    context.SaveChanges();
    return Request.CreateResponse(HttpStatusCode.OK, "Delete successfully CategoryID = "     +     CategoryID);
    }
    else …
Run Code Online (Sandbox Code Playgroud)

using http-post dotnet-httpclient

1
推荐指数
1
解决办法
2958
查看次数

标签 统计

dotnet-httpclient ×1

http-post ×1

using ×1