【问题标题】:How to call Get Method with Send FromBody from Windows Application如何从 Windows 应用程序使用 Send FromBody 调用 Get 方法
【发布时间】:2019-06-11 04:30:47
【问题描述】:

我有一个 API 接受 get 方法中的 [FromBody] 那么我如何调用这个 Get 方法

我的 API 是

     [BearerAuth]
     [HttpGet]
     public Dictionary<long, string> GetEmployees([FromBody] Employee obj)
    {
        Dictionary<long, string> dic = new Dictionary<long, string>();

        //Some Implemenation

        return dic;
    }

现在我正在尝试从 Windows 应用程序调用它

            using (WebClient webRequest = new WebClient())
            {
                Employee emp = new Employee { EmpId = 1, Name = "Mohan", Gender = "Male", MobileNo = "9560498289", Salary = 50000 };
                string url = APIUrl_2 + "/APITest/GetMethod_FromBody";

                //string JsonString = JsonConvert.SerializeObject(emp);
                //ASCIIEncoding encoding = new ASCIIEncoding();
                //byte[] data = encoding.GetBytes(JsonString);

                WebRequest request = WebRequest.Create(url);
                request.Method = "GET";
                request.Headers["Authorization"] = "Bearer " + "AuthKey";
                using (WebResponse response = request.GetResponse())
                {

                }
              }

请谁能帮我在 FromBody 中发送员工数据,它被调用但 Employee 为空。

我只能通过get方法来做。

提前致谢

【问题讨论】:

    标签: .net c#-4.0


    【解决方案1】:

    [FromBody] 属性不能用于 [HttpGet] 修饰方法。 为了修复代码,您可以进行两种可能的修改:

    1. 第一个选项:将您的方法转换为 POST 并在请求正文中发送您的员工对象:

       [BearerAuth]
       [HttpPost]
       public Dictionary<long, string> GetEmployees([FromBody] Employee obj)
       {
          Dictionary<long, string> dic = new Dictionary<long, string>();
      
          //Some Implemenation
      
          return dic;
       }
      

    还有你的客户端代码:

         using (WebClient webClient = new WebClient())
         {
            //add authorization
            Employee emp = new Employee { EmpId = 1, Name = "Mohan", Gender = "Male", MobileNo = "9560498289", Salary = 50000 };
            string url = APIUrl_2 + "/APITest/GetMethod_FromBody";
    
            string jsonString = JsonConvert.SerializeObject(emp);
    
            string response = webClient.UploadString(url, jsonString);
         }
    
    1. 第二个选项:将您的 [FromBody] 转换为 [FromUri] 并将您的员工对象作为查询字符串发送

       [BearerAuth]
       [HttpGet]
       public Dictionary<long, string> GetEmployees([FromGet] Employee obj)
       {
          Dictionary<long, string> dic = new Dictionary<long, string>();
      
          //Some Implemenation
      
          return dic;
       }
      

    客户的相关部分:

    webClient.QueryString.Add("EmptId", emp.EmpId);
    webClient.QueryString.Add("Name", emp.Name);
    webClient.QueryString.Add("Gender", emp.Gender);
    ...
    string response = webClient.DownloadString(url);
    

    【讨论】:

    • 谢谢我理解这个东西,但我认为我们可以像在 Post Method 中使用的那样使用 get methof
    猜你喜欢
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2017-06-27
    • 1970-01-01
    • 2017-06-04
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    相关资源
    最近更新 更多