我对API的整个群体非常陌生。到目前为止,我设法构建了一个已获得,发布,放置和删除方法的Web API。
现在,从ASP.NET项目中,我尝试最终使用我的Web API。
这是我为获得方法所做的:
string info = new WebClient() { }.DownloadString("https://mywebapisite.com/item/" + id);
Item item = JsonConvert.DeserializeObject<Item>(info);
这一切都很好。如您所见,所有GET方法的需求都是ID。
但是,对于帖子方法,我不知道该怎么办。
我可以创建一个新的 item
实例,但不知道该如何处理。
顺便说一句,我还使用asp.net制作我的web.api。
ASP.NET 5中有一个内置功能,称为Swagger。它可以非常成功地执行所有任务。是否有代码的代码。
ps:我知道这个问题必须非常普遍和基本。如果您可以在Stackoverflow中推荐我的另一个问题,或者只是告诉我在Google上搜索什么,我将不胜感激。 (您可能猜到,我什至不知道要搜索什么)
I am very new to the whole consept of API's. So far, I managed to build a web api that has GET,POST,PUT and DELETE methods.
Now, from an ASP.NET project, I try to finally use my web api.
Here's what I do for GET method:
string info = new WebClient() { }.DownloadString("https://mywebapisite.com/item/" + id);
Item item = JsonConvert.DeserializeObject<Item>(info);
This functions all fine. As you can see, all the GET method needs is an id.
However, for the POST method, I have no clue what to do.
I can create a new Item
instance, but don't know what to do with it.
By the way, I also used ASP.NET to make my web.api.
There is a built-in feature in ASP.NET 5 called Swagger. It can perform all the tasks very succesfully. Is there like a code-behind for what Swagger does.
PS: I know that this question must be very common and basic. If you could refer me to another question in stackoverflow or simply tell me what to search on google I would appreciate it. (As you may guess, I don't even know what to search for)
发布评论
评论(3)
伪代码在C#中消耗发布请求
pseudo code to consume post request in C#
引用以下代码来调用API
您可以使用httpclient:这样的API方法
:和这样的结果:
您也可以参考此链接设置content-type。
You can refer the following code to call the API using HttpClient:
The API method like this:
And the result like this:
You can also refer this link to set the Content-Type.
您似乎有点迷路了,我明白了。 API学习路径很奇怪,建议您观看教程(我最喜欢的)
但是,如果您尽快需要代码,则可以参考以下代码。
PS:其他答案真的很好!
You seem a little bit lost, and I get it. Api learning path is kinda weird, I recommend you watch a tutorial (My favorite https://www.youtube.com/playlist?list=PLLWMQd6PeGY0bEMxObA6dtYXuJOGfxSPx)
But if you need code asap, you could refer the following code.
Ps: The others answers are really good!