Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 带有Json参数的RESTAPI Get请求_C#_Rest_Api_Asp.net Web Api - Fatal编程技术网

C# 带有Json参数的RESTAPI Get请求

C# 带有Json参数的RESTAPI Get请求,c#,rest,api,asp.net-web-api,C#,Rest,Api,Asp.net Web Api,我有一个任务,我需要使用复杂类型参数请求web apiGET请求,我想我们不能做这样的事情,因为GET请求希望通过URL共享所有内容 有谁能帮助我实现这一目标吗。通过C#使用带有JSON数据的Web API GET请求 消费者控制台: class Program { static void Main(string[] args) { try { // Need to pass

我有一个任务,我需要使用复杂类型参数请求web apiGET请求,我想我们不能做这样的事情,因为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; }
    }
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; }
    }
Web API:

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; }
    }
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请求中发送正文中的数据。因此,您可以使用[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;
    }

这些链接可能会帮助您添加有问题的复杂类型或示例请求format@MdFaridUddinKiron用我尝试过的代码更新了我的问题IDE注1:GET参数中的复杂类型总是一个坏主意,也是一个架构错误。旁注2:使用而不是
WebRequest
。您希望以相同的格式请求,还是我可以自定义它?您可以通过控制台应用程序分享如何使用此应用程序吗