在ASP.NET MVC 5中使用api

编程入门 行业动态 更新时间:2024-10-24 23:28:25
本文介绍了在ASP.NET MVC 5中使用api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

大家好, 美好的一天! 我在使用剩余的api时遇到关于PutAsJsonAsync和PostAsJsonAsync的问题:afs-test-redis99.afservice:8080/site/ 操作:PUT 名称:addItem 路径:/ api / cart / {id} / item 查询:商店类型 参数:item,quantity,key 请求正文:AnyObject(可选) 示例:在我的邮递员中,当我尝试添加/添加新项目时它工作正常: afs-test-redis99.afservice:8080/site / api / cart / 51 / item?item = COM1& store = PCM& type = active& quantity = 5 但是当我试图在我的mvc存储​​库类

Hi to all, Good day! I'm having an issue regarding the PutAsJsonAsync and PostAsJsonAsync in consuming the rest api: "afs-test-redis99.afservice:8080/site/" Operations: PUT Name: addItem Path: /api/cart/{id}/item Query: store type Parameters: item, quantity,key Request Body: AnyObject(optional) Example: In my postman, it is working correctly, when I tried to put/add new item: "afs-test-redis99.afservice:8080/site/api/cart/51/item?item=COM1&store=PCM&type=active&quantity=5" But when I tried to implement this in my mvc repository class

public async Task<Uri> SaveItemAsync(Item item) { client.BaseAddress = new Uri("afs-test-redis99.afservice:8080/site/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue( "application/json")); HttpResponseMessage response = await client.PutAsJsonAsync($"api/cart/{item.ID}/item?",$"item={item.ProductID}&store=PCM&type=active&{item.Quantity}"); response.EnsureSuccessStatusCode(); // return URI of the created resource. return response.Headers.Location; }

结果错误是:

The result error is:

{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Connection: close Date: Tue, 13 Jun 2017 00:23:21 GMT Server: Apache-Coyote/1.1 Content-Length: 1062 Content-Language: en Content-Type: text/html; charset=utf-8 }}

任何人都可以帮助我解决我的代码出了什么问题,抱歉我是新来的在我的客户端应用程序中使用web api。非常感谢您提前。 我尝试过: 它在邮递员中正常工作:

Can anyone assist me what's wrong on my code, sorry I'm new to consuming the web api in my client application. Thank you very much in advance. What I have tried: It is working correctly in postman:

"afs-test-redis99.afservice:8080/site/api/cart/51/item?item=COM1&store=PCM&type=active&quantity=5" But when I tried to implement this in my mvc repository class (c#) public async Task<Uri> SaveItemAsync(Item item) { client.BaseAddress = new Uri("afs-test-redis99.afservice:8080/site/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue( "application/json")); HttpResponseMessage response = await client.PutAsJsonAsync($"api/cart/{item.ID}/item?",$"item={item.ProductID}&store=PCM&type=active&{item.Quantity}"); response.EnsureSuccessStatusCode(); // return URI of the created resource. return response.Headers.Location; }

推荐答案

api / cart / {item.ID} / item?, "api/cart/{item.ID}/item?",

\"item={item.ProductID}&store=PCM&type=active&{item.Quantity}); response.EnsureSuccessStatusCode(); //返回已创建资源的URI。 返回response.Headers.Location; } "item={item.ProductID}&store=PCM&type=active&{item.Quantity}"); response.EnsureSuccessStatusCode(); // return URI of the created resource. return response.Headers.Location; }

结果错误是:

The result error is:

{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Connection: close Date: Tue, 13 Jun 2017 00:23:21 GMT Server: Apache-Coyote/1.1 Content-Length: 1062 Content-Language: en Content-Type: text/html; charset=utf-8 }}

任何人都可以帮助我解决我的代码出了什么问题,抱歉我是新来的在我的客户端应用程序中使用web api。非常感谢您提前。 我尝试过: 它在邮递员中正常工作:

Can anyone assist me what's wrong on my code, sorry I'm new to consuming the web api in my client application. Thank you very much in advance. What I have tried: It is working correctly in postman:

"afs-test-redis99.afservice:8080/site/api/cart/51/item?item=COM1&store=PCM&type=active&quantity=5" But when I tried to implement this in my mvc repository class (c#) public async Task<Uri> SaveItemAsync(Item item) { client.BaseAddress = new Uri("afs-test-redis99.afservice:8080/site/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue( "application/json")); HttpResponseMessage response = await client.PutAsJsonAsync(

api / cart / {item.ID} / item?, "api/cart/{item.ID}/item?",

更多推荐

在ASP.NET MVC 5中使用api

本文发布于:2023-11-11 06:27:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577626.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NET   ASP   api   MVC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!