在ASP.NET MVC 5中使用api [英] Consuming api in ASP.NET MVC 5

查看:79
本文介绍了在ASP.NET MVC 5中使用api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

美好的一天!



我在使用剩余的api时遇到关于PutAsJsonAsync和PostAsJsonAsync的问题:http://afs-test-redis99.afservice.org:8080/site/





操作:PUT



名称:addItem

路径:/ api / cart / {id} / item

查询:商店类型

参数:item,quantity,key

请求正文:AnyObject(可选)



示例:在我的邮递员中,当我尝试添加/添加新项目时它工作正常:



http://afs-test-redis99.afservice.org: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: "http://afs-test-redis99.afservice.org: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:

"http://afs-test-redis99.afservice.org: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("http://afs-test-redis99.afservice.org: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:

"http://afs-test-redis99.afservice.org: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("http://afs-test-redis99.afservice.org: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:

"http://afs-test-redis99.afservice.org: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("http://afs-test-redis99.afservice.org: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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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