【问题标题】:How to call Post method with multiple parameter from C# project如何从 C# 项目调用具有多个参数的 Post 方法
【发布时间】:2019-12-09 12:00:21
【问题描述】:

我已经创建了一个 web api 项目,在值控制器中我创建了一个方法 InsertHeading,它接受三个参数并返回一个唯一的 id。该方法如下所示:-

  public int InsertHeading([FromBody]string appid, [FromBody]string type, [FromBody]string detail)
        {
            int x = 1;
            return 1;


        }

我也试过这个变种

[HttpPost]
      public int InsertHeading(string appid, string type, string detail)
            {
                int x = 1;
                return 1;
            }

当我给出如下网址时,这段代码正在运行:-http://server:port/LoggingAPi/Values/InsertHeading 来自肥皂用户界面。

但是当我尝试从我的 c# 代码中调用此方法时,我遇到了 404 错误。这是我尝试过的两种方法:-

        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri("http://xxxxx:45422/");
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            var append = new MasterLogInfo() { appid = "2", type = "request", detail = "test call from genesys" };

            HttpResponseMessage response = await client.PostAsJsonAsync("LoggingAPI/Values/InsertMasterloginfo", append);

            if (response.IsSuccessStatusCode)
            {
                // Get the URI of the created resource.
                Uri finalURL = response.Headers.Location;
            }

        }

方法二:-

//  client.BaseAddress = new Uri("http://localhost:53117/");
                client.BaseAddress = new Uri("http://xxxxxx:45422/");
                client.DefaultRequestHeaders.Accept.Clear();
            //    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                client.DefaultRequestHeaders.Accept.Add(
   new MediaTypeWithQualityHeaderValue("application/json"));
                var user = new MasterLogInfo();
                user.appid = "100";
                user.typeRequest = "Test";
                user.detail = "test call from genesys";

      var response = client.PostAsync("LoggingAPI/Values/InsertMasterloginfo", new StringContent(new JavaScriptSerializer().Serialize(user), Encoding.UTF8, "application/json")).Result;

            //    var response = client.PostAsync("Values/InsertHeading", new StringContent(new JavaScriptSerializer().Serialize(user), Encoding.UTF8, "application/json")).Result;


                if (response.IsSuccessStatusCode)
                {
                    // Get the URI of the created resource.
                    Uri finalURL = response.Headers.Location;
                }

            }

如果我在参数中使用 FromBody 标记,我得到 500 内部服务器错误,没有它我得到 404 错误。谁能告诉我我错过了什么。出于安全目的,我删除了插入标题的正文

【问题讨论】:

    标签: c# .net webapi


    【解决方案1】:

    创建一个对象,该对象表示来自帖子的有效负载。然后你可以在动作参数中使用它,例如public int InsertHeading([FromBody] MyObject myObject)

    【讨论】:

    • 函数调用部分需要做哪些修改?否则,如果我使用上述方式,我仍然会收到 500 内部服务器错误
    • 将端点从LoggingAPI/Values/InsertMasterloginfo更改为LoggingAPI/Values/InsertHeading
    • 那只是为了测试,我一直在使用 insertHeading 而已
    • 您是从另一个 Web 项目调用 API,如果是,500 是来自您调用的 API,还是调用的 API?
    • 是的,api 插入标题不同。它旨在供多个其他 Web 服务使用。我正在消费的那个是完全不同的,并且在当前场景中是控制台应用程序。 500 内部服务器即将到来,以防调用主 Web 服务'
    猜你喜欢
    • 1970-01-01
    • 2011-10-14
    • 1970-01-01
    • 1970-01-01
    • 2014-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多