如何使用asp.net的webApi? [英] How to consume a webApi from asp.net?

查看:103
本文介绍了如何使用asp.net的webApi?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做一个从数据库调用api的项目

I need to do project which call api's from database

创建另一个项目,并调用仅在其控制器中包含api的旧项目

and make another project and call the old one which contain api's only in its controller

我遇到那些问题1-我想从需要对象并返回对象的api返回一个对象

I face those problems 1- I want to return an object from api which takes object and return object as

这是我的api,它位于"testController"中

this is my api which locate in "testController"

  [HttpPost]
            [ActionName("fetch_information")]
            public login_info fetch_information(login_info_request request)

,我想从另一个项目中调用此api

and I want to call this api from another project as

      public login_info fetch_information(login_info_request request)
            {

                HttpClient client = new HttpClient();
                client.BaseAddress = new Uri("http://localhost:58295/fetch_information");


                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));


                HttpResponseMessage response = client.GetAsync("api/"+request+"").Result;
                if (response.IsSuccessStatusCode)
                {              
                    return null;
  here I want to return an object of "login_info " 
                }
                else
                {
                    return null;
                }
            }

我的问题是我在哪里可以给它请求对象数据"login_info_request"?哪里可以从api"login_info"接收对象?

my question is where can I give it request object data "login_info_request "? and where can I recieve object from api "login_info"?

提前感谢

推荐答案

代码的问题是您发出了GET请求,而所显示的Web API方法却期望POST.

The problem with your code is that you made a GET request whereas the Web API method you have shown expects POST.

所以:

HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:58295/fetch_information");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

login_info_request req = ... 
string postBody = JsonConvert.SerializeObject(req);
HttpContent content = new StringContent(postBody, Encoding.UTF8, "application/json");
HttpResponseMessage response = client.PostAsync("api/" + request, content).Result;
if (response.IsSuccessStatusCode)
{
    // Read the response body as string
    string json = response.Content.ReadAsStringAsync().Result;

    // deserialize the JSON response returned from the Web API back to a login_info object
    return JsonConvert.DeserializeObject<login_info>(json);
}
else
{
    return null;
}

如果您使用 Microsoft ASP.NET Web API 2.2客户端库NuGet 中有一个

And if you use the Microsoft ASP.NET Web API 2.2 Client Libraries NuGet there is an extension method which will allow you to shorten the code:

if (response.IsSuccessStatusCode)
{
    return response.Content.ReadAsAsync<login_info>().Result;
}
else
{
    return null;
}

这篇关于如何使用asp.net的webApi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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