本文介绍了如何将JSON数据从fiddler发布到.net中的api Controller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个模型类产品:
公共类产品
{
public int Id {get;组; }
public string Name {get;组; }
public string Category {get;组; }
公共小数价格{get;组; }
}
和API控制器是ProductsController,Post方法是:
[HttpPost]
public HttpResponseMessage PostProduct(Product item)
{
if(item == null)
返回新的HttpResponseMessage(HttpStatusCode.BadRequest);
item = repository.Add(item);
var response = Request.CreateResponse< Product>(HttpStatusCode.Created,item);
string uri = Url.Link(DefaultApi,new {id = item.Id});
response.Headers.Location = new Uri(uri);
返回回复;
}
当我从fiddler Product项发布JSON数据时,我得到null值,但get方法正常。
请有人帮我解决这个问题。
解决方案
I have a model class Product as:
public class Product { public int Id { get; set; } public string Name { get; set; } public string Category { get; set; } public decimal Price { get; set; } }
and the API controller is ProductsController and the Post method is:
[HttpPost] public HttpResponseMessage PostProduct(Product item) { if (item == null) return new HttpResponseMessage(HttpStatusCode.BadRequest); item = repository.Add(item); var response = Request.CreateResponse<Product>(HttpStatusCode.Created, item); string uri = Url.Link("DefaultApi", new { id = item.Id }); response.Headers.Location = new Uri(uri); return response; }
When I post JSON data from fiddler Product item, I get null value, but get method works fine.
Please anyone help me to solve this problem.
解决方案
这篇关于如何将JSON数据从fiddler发布到.net中的api Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!