无法在web api中传递json对象 [英] Unable to pass json object in web api

查看:93
本文介绍了无法在web api中传递json对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从POSTMAN请求行json工作正常

Ex-当我传递siple 1工作但是当我发送它像{id:1} m没有得到结果



我尝试过:



When I request From POSTMAN with row json its working fine
Ex -when i pass siple 1 its working but when i send this like {"id":"1"} m not getting the result

What I have tried:

namespace DemoApi.Controllers
{
    public class EmployeeController : ApiController
    {       
        IList employees = new List() 
        {
           new Employee()  
                {  
                    EmployeeId = 1, EmployeeName = "Mukesh Kumar", Address = "New Delhi", Department = "IT"  
                },  
                new Employee()  
                {  
                    EmployeeId = 2, EmployeeName = "Banky Chamber", Address = "London", Department = "HR"  
                },  
                new Employee()  
                {  
                    EmployeeId = 3, EmployeeName = "Rahul Rathor", Address = "Laxmi Nagar", Department = "IT"  
                },  
                new Employee()  
                {  
                    EmployeeId = 4, EmployeeName = "YaduVeer Singh", Address = "Goa", Department = "Sales"  
                },  
                new Employee()  
                {  
                    EmployeeId = 5, EmployeeName = "Manish Sharma", Address = "New Delhi", Department = "HR"  
                },  
        };

        public IList GetAllEmployees()
        {
            //Return list of all employees  
            return employees;
        }       

        [HttpPost]       
        public Employee GetData([FromBody] int id)
        {
            var employee = employees.FirstOrDefault(e => e.EmployeeId == id);

            if (employee == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }
            return employee;
        }

    }
}

推荐答案

使用[FromBody]



要强制Web API从请求正文中读取简单类型,请将[FromBody]属性添加到参数:



public HttpResponseMessage Post([FromBody]字符串名称){...}

在此示例中,Web API将使用媒体类型格式化程序来读取值来自请求正文的名称。这是一个示例客户端请求。



POST http:// localhost:5076 / api / values HTTP / 1.1

User-Agent: Fiddler

主持人:localhost:5076

内容类型:application / json

内容长度:7

Alice



当参数有[FromBody]时,Web API使用Content-Type标题选择格式化程序。在此示例中,内容类型为application / json,请求正文是原始JSON字符串(不是JSON对象)。



最多一个参数是允许从邮件正文中读取。所以这不起作用:



//警告:不起作用!

Using [FromBody]

To force Web API to read a simple type from the request body, add the [FromBody] attribute to the parameter:

public HttpResponseMessage Post([FromBody] string name) { ... }
In this example, Web API will use a media-type formatter to read the value of name from the request body. Here is an example client request.

POST http://localhost:5076/api/values HTTP/1.1
User-Agent: Fiddler
Host: localhost:5076
Content-Type: application/json
Content-Length: 7
"Alice"

When a parameter has [FromBody], Web API uses the Content-Type header to select a formatter. In this example, the content type is "application/json" and the request body is a raw JSON string (not a JSON object).

At most one parameter is allowed to read from the message body. So this will not work:

// Caution: Will not work!
public HttpResponseMessage Post([FromBody] int id, [FromBody] string name) { ... }





这个规则的原因是请求体可能存储在一个只能读取一次的非缓冲流中。



在这里阅读更多: ASP.NET Web API中的参数绑定ASP.NET站点 [ ^ ]


这篇关于无法在web api中传递json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆