发送C#对象的WebAPI控制器 [英] Sending C# object to webapi controller
问题描述
我想一个C#对象传递到Web API控制器。 API被配置为存储投递到这类型的产品对象。我使用Jquery的Ajax方法成功添加的对象,现在我试图让在C#中相同的结果。
我创建了一个简单的控制台应用程序发送POST请求的API:
公共类产品
{
公众诠释标识{搞定;组; }
公共字符串名称{;组; }
公共字符串类别{搞定;组; }
公共十进制价格{搞定;组; }
} 静态无效的主要(字串[] args)
{
字符串apiUrl = @的http://本地主机:3393 / API /产品;
VAR的客户=新的HttpClient();
client.PostAsJsonAsync<产品及GT;(apiUrl,新产品(){ID = 2,名称=牛仔裤,价格= 200,类别=服装}); }
该postproduct方法是永远不会调用,如何将这些对象发送到控制器?
用于添加物品的方法:
公开的Htt presponseMessage PostProduct([FromBody]产品项)
{
项目= repository.Add(项目);
VAR响应= Request.CreateResponse<产品及GT;(的HTTPStatus code.Created,项目); 字符串URI = Url.Link(DefaultApi,新{ID = item.Id});
response.Headers.Location =新的URI(URI);
返回响应;
}
看起来你已经以某种方式禁用接受JSON作为发布格式。我能够将数据发送到您的端点和使用创造新产品应用程序/ x-WWW的形式urlen codeD
。这可能是你的jQuery请求是怎么做的吧。
您可以显示你的网页API的配置code?你更改默认格式化?
或者你可以从HttpClient的发送形式。例如。
字符串apiUrl =http://producttestapi.azurewebsites.net/api/products;
VAR的客户=新的HttpClient();
VAR值=新词典<字符串,字符串>()
{
{ID,6},
{姓名,滑雪},
{价格,100},
{类别,体育}
};
VAR内容=新FormUrlEn codedContent(值); VAR响应=等待client.PostAsync(apiUrl,内容);
response.EnsureSuccessStatus code();
I'm trying to pass a C# object to a web api controller. The api is configured to store objects of type Product that are posted to it. I have successfully added objects using Jquery Ajax method and now I'm trying to get the same result in C#.
I've created a simple Console application to send a Post request to the api:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public string Category { get; set; }
public decimal Price { get; set; }
}
static void Main(string[] args)
{
string apiUrl = @"http://localhost:3393/api/products";
var client = new HttpClient();
client.PostAsJsonAsync<Product>(apiUrl, new Product() { Id = 2, Name = "Jeans", Price = 200, Category = "Clothing" });
}
The postproduct method is never invoked, how to send this object to the controller ?
Method used for adding items:
public HttpResponseMessage PostProduct([FromBody]Product item)
{
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;
}
Looks like you have somehow disabled accepting JSON as format for posting. I was able to send the data to your endpoint and create new product using application/x-www-form-urlencoded
. That is probably how your jQuery request is doing it.
Can you show your configuration code of your web api? Do you change the default formatters?
Or you could send a form from HttpClient. e.g.
string apiUrl = "http://producttestapi.azurewebsites.net/api/products";
var client = new HttpClient();
var values = new Dictionary<string, string>()
{
{"Id", "6"},
{"Name", "Skis"},
{"Price", "100"},
{"Category", "Sports"}
};
var content = new FormUrlEncodedContent(values);
var response = await client.PostAsync(apiUrl, content);
response.EnsureSuccessStatusCode();
这篇关于发送C#对象的WebAPI控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!