简体   繁体   English

如何引用已经在 API 级别声明的 Object? (asp.net web-api)

[英]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.我对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?我想知道以下内容:有没有办法引用在 API 级别声明的对象(客户、学生等...)?

Let's say I'm going to consume the web-api from a console app.假设我将从控制台应用程序使用 web-api。 Once I make a call to the web api like this:一旦我像这样调用 web api:

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?引用已经在 API 级别声明的 Student 的最佳实践是什么?

Compared to WCF, when the service-reference is added, you get the Objects/Entities declared at the service level.与 WCF 相比,添加服务引用后,您将获得在服务级别声明的对象/实体。 Is there something similar in asp.net web-api? asp.net web-api 中有类似的东西吗?

You can do it manually:您可以手动完成:

1) Copy the Json/Xml text returned by your API. 1) 复制您的 API 返回的 Json/Xml 文本。

2) Open Visual Studio and click in Edit > Paste Special > (Paste XML As Classes or Paste as JSON Classes). 2) 打开 Visual Studio 并单击“编辑”>“选择性粘贴”>(将 XML 粘贴为类或粘贴为 JSON 类)。

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.我认为,如果 Web API 是一个外部资源,它不确定与使用 API 的其他项目的库,那么始终在客户端创建模型响应是一个不错的选择。

In that way you'll avoid to use dynamic objects and the code will be more readable as well.这样,您将避免使用动态对象,并且代码也将更具可读性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在ASP.net Web-API中使用CustomExceptionException处理程序 - How to use customexceptionhandlers in ASP.net web-api asp.net Web-Api JSON字符串反序列化 - asp.net Web-Api JSON string deserialization Asp.net为web-api解雇重定向的核心授权 - Asp.net core authorization for web-api firing redirect ASP.net Web-api中的简单类型JSON序列化 - Simple types JSON serialization in ASP.net Web-api 如何通过ID发送消息到signalR特定的PersistentConnection? (使用asp.net mvc4 web-api) - How to send message to signalR specific PersistentConnection by its ID ? (with asp.net mvc4 web-api) 服务ASP.NET Web-API HTTP请求涉及多少个线程? - How many threads are involved in servicing a ASP.NET Web-API HTTP request? 如何从ASP.NET Web-API中的另一个方法调用Windows中的方法 - how to call a method in a windows from another method in asp.net web-api 如何在ASP.NET Web-API项目中将FluentValidation与LightInject一起使用 - How to use FluentValidation with LightInject in asp.net web-api project 如何接收发布到 ASP.net 核心 2.1 Web-API 端点的文件和数据 - How to receive both File and data POSTed to an ASP.net core 2.1 Web-API endpoint PHP / ASP.NET MVC Web-Api有人知道如何使用PHP通过POST调用WEBAPI - PHP/ASP.NET MVC Web-Api Somebody Know How To Use PHP To Call WEBAPI With POST
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM