简体   繁体   中英

How to reference Object that it has been already declared at the API level? (asp.net web-api)

I'm relatively new to asp.net Web API. I would like to know the following: Is there a way to reference an Object (Customer, Student, etc...) declared at the API level?

Let's say I'm going to consume the web-api from a console app. Once I make a call to the web api like this:

using(HttpResponseMessage response = ApiHelper.ApiClient.GetAsync(ApiHelper.ApiClient.BaseAddress).Result)
        {
            if (response.IsSuccessStatusCode)
            {
                List<Student> hello = await response.Content.ReadAsAsync<List<Student>>();

                return hello;
            }

What is the best practice to reference Student that it has been already declared at the API level?

Compared to WCF, when the service-reference is added, you get the Objects/Entities declared at the service level. Is there something similar in asp.net web-api?

You can do it manually:

1) Copy the Json/Xml text returned by your API.

2) Open Visual Studio and click in Edit > Paste Special > (Paste XML As Classes or Paste as JSON Classes).

I think it is a good option always to create the Model response in the client-side if the Web API is an external resource that does not sure libraries with other projects that use the API.

In that way you'll avoid to use dynamic objects and the code will be more readable as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM