c# - Rest API Get请求带Json参数

标签 c# rest api asp.net-web-api

我有一个任务,我需要使用复杂类型参数请求Web api GET请求,我想我们无法执行GET请求之类的事情希望所有内容都通过 URL 共享。

任何人都可以帮助我实现这一目标吗?通过 C# 使用带有 JSON 数据的 Web API GET 请求。

消费者控制台:

class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // Need to pass this through GET Request
                var employee = new Employee() { EmployeeId = 1, EmployeeName = "Test", Designation = "Developer", Salary = 100 };
                var jsonParam = JsonConvert.SerializeObject(employee);
                //


                var request = (HttpWebRequest)WebRequest.Create("http://localhost:52237/Values/GetEmp");                

                var encoding = new UTF8Encoding();
                var bytes = encoding.GetBytes(jsonParam);

                request.Method = "GET";
                request.ContentLength = bytes.Length;
                request.ContentType = "application/json";

                using (var writeStream = request.GetRequestStream())
                {
                    writeStream.Write(bytes, 0, bytes.Length);
                }

                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    var responseValue = string.Empty;

                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        // grab the response
                        using (var responseStream = response.GetResponseStream())
                        {
                            if (responseStream != null)
                                using (var reader = new StreamReader(responseStream))
                                {
                                    responseValue = reader.ReadToEnd();
                                }
                        }
                    }
                }              
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
    public class Employee
    {
        public int EmployeeId { get; set; }
        public string EmployeeName { get; set; }
        public int Salary { get; set; }
        public string Designation { get; set; }
    }

Web API:

public class ValuesController : ApiController
    {        
        [HttpGet]
        [Route("api/GetEmp")]
        public Employee GetEmp([FromUri]Employee employee)
        {
            // Getting employee object from client

            // Yet to implement

            if (employee != null)
            {
                employee.Designation = "Engineer";
            }
            return employee;
        }
    }

    public class Employee
    {
        public int EmployeeId { get; set; }
        public string EmployeeName { get; set; }
        public int Salary { get; set; }
        public string Designation { get; set; }
    }

提前致谢。

最佳答案

我认为在HTTP 1.1版本之后你也可以在GET请求的body中发送数据。因此,您可以使用 [FromBody] 来代替 [FromUri]。

    [HttpGet]
    [Route("api/GetEmp")]
    public Employee GetEmp([FromBody]Employee employee)
    {
        // Getting employee object from client

        // Yet to implement

        if (employee != null)
        {
            employee.Designation = "Engineer";
        }
        return employee;
    }

关于c# - Rest API Get请求带Json参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57156750/

相关文章:

api - 如何像 Wine 在 Linux 中一样记录 Windows 中的所有 API 调用?

c# - WCF中的*是什么意思

c# - 从经过身份验证的 URL 传输音频

javascript - 访问控制允许来源不允许 AJAX 来源 null

休息服务 - Tomcat 能够启动,主页工作正常但不能执行任何 GET

REST API 分页 : make page-size a parameter (configurable from outside)

android - 我可以使用什么 API 制作 Android 应用程序来检测 Eddystone 信标?

java - IBM RTC API - 将文件添加到更改集

C# - 在运行时更新 pictureBox.Image 导致错误

c# - 根据现有值更改 dataGridView 中的值