简体   繁体   English

我可以将 json 对象从模型类发布到 angular .ts 文件吗

[英]Can I post json object from model class to angular .ts file

I am retrieving XML result from thrid-party API to ASP.net web API.我正在从第三方 API 检索 XML 结果到 ASP.net Web API。 Now I want to send this retrieved XML( converted in JSON) to angular typescript file.现在我想将此检索到的 XML(以 JSON 格式转换)发送到 angular typescript 文件。 How can I achieve it?我怎样才能实现它?

My approach 1. send JSON from model class to controller and then from the controller to angular service or component's .ts file.我的方法 1. 将 JSON 从模型类发送到控制器,然后从控制器发送到角度服务或组件的 .ts 文件。 2.post from model class to component's .ts file. 2.从模型类发布到组件的 .ts 文件。

     var objJson = new JavaScriptSerializer().Serialize(responseB);
     var response = await client.PostAsync("http://localhost:4200/add", new StringContent(objJson, Encoding.UTF8, "application/json"));
     var responseString = await response.Content.ReadAsStringAsync();

You can't send something "from a WebAPI to a TypeScript file".您不能“从 WebAPI 发送到 TypeScript 文件”。

It sounds like you want to send it from the server to a client app in a web browser?听起来您想将它从服务器发送到 Web 浏览器中的客户端应用程序? If this is the case, then you need the client app to ask for it, or to push it to the client.如果是这种情况,那么您需要客户端应用程序来请求它,或者将它推送给客户端。

If the client initiated the request that had you fetch this resource, then you could send it in the response to the client.如果客户端发起了让您获取此资源的请求,那么您可以在响应中将其发送给客户端。

If the client app has some bi-directional connection to your server like socket.io or SignalR , you can send it over that.如果客户端应用程序与您的服务器有某种双向连接,例如socket.ioSignalR ,您可以通过它发送它。

Or, depending on the frequency, probability of this happening, you might have the client poll for it.或者,根据发生这种情况的频率和概率,您可以让客户端对其进行轮询。

But it all depends on the state machine of the entire process you are implementing.但这一切都取决于您正在实施的整个过程的状态机。 Does the client app initiate any of this, or this an event that the client does not initiate, but needs to be informed of?客户端应用程序是否启动了其中的任何一个,或者这是一个客户端未启动但需要通知的事件? If this is the case, you either need to poll or to set up a connection from the client to the server.如果是这种情况,您需要轮询或建立从客户端到服务器的连接。

如何访问列表<object>来自 appsettings.Json?<div id="text_translate"><p> 在我的 appsettings.json 我有一个这样的字段</p><pre> "MyFields": [ { "name":"one", "type":"type1" "parameters":[{"key":"url","value":"myurl.com"}, {"key":"data","value":"mydata"}] }, { "name":"two", "type":"type2"... .. and so on } ]</pre><p> 我制作了一个具有以下属性的 class Myfield:</p><pre> public class MyField { [JsonProperty] public string? name { get; set; } [JsonProperty] public string? type { get; set; } [JsonProperty] public IDictionary<string,string>? parameters { get; set; } }</pre><p> 我正在尝试使用配置在另一个 class 中访问它,如下所示:</p><pre> //config is the configuration var myFields = config.GetSection("MyFields").Get<List<MyField>>();</pre><p> 问题是 myFields 结果是空的。 但是当我通过消除“参数”字段来做同样的事情时,它就像一个魅力。</p><p> 我知道这与匹配不当有关,但能得到一些帮助会很棒。</p></div></object> - How can I access a List<object> from appsettings.Json?

暂无
暂无

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

相关问题 我如何使用获取将json对象和文件发布到asp.net服务器 - How can I post a json object and a file to asp.net server with fetch 从 model class 获取 null 值,该值与 Z466DEEC76ECDF5FCA6D38571F63file24D54 绑定 - Getting null value from model class which is bind with a json file ASP.NET:开机自检列表 <Object> /使用Ajax(JSON)从视图到控制器进行建模 - ASP.NET: POST List<Object> / Model with Ajax (JSON) from view to controller ts服务文件中的Angular2 API控制器调用 - Angular2 api controller call from ts service file Model 绑定无法绑定字符串数组,当发布为 JSON object - Model binding is not able to bind string array when post as JSON object 如何访问列表<object>来自 appsettings.Json?<div id="text_translate"><p> 在我的 appsettings.json 我有一个这样的字段</p><pre> "MyFields": [ { "name":"one", "type":"type1" "parameters":[{"key":"url","value":"myurl.com"}, {"key":"data","value":"mydata"}] }, { "name":"two", "type":"type2"... .. and so on } ]</pre><p> 我制作了一个具有以下属性的 class Myfield:</p><pre> public class MyField { [JsonProperty] public string? name { get; set; } [JsonProperty] public string? type { get; set; } [JsonProperty] public IDictionary<string,string>? parameters { get; set; } }</pre><p> 我正在尝试使用配置在另一个 class 中访问它,如下所示:</p><pre> //config is the configuration var myFields = config.GetSection("MyFields").Get<List<MyField>>();</pre><p> 问题是 myFields 结果是空的。 但是当我通过消除“参数”字段来做同样的事情时,它就像一个魅力。</p><p> 我知道这与匹配不当有关,但能得到一些帮助会很棒。</p></div></object> - How can I access a List<object> from appsettings.Json? 我如何从C#中的类对象提取字段 - How can i extract fields from a class object in c# 将JSON对象转换为WEB API后操作方法中的类成员 - Conversion of JSON object into class members in WEB API Post Action Method 如何从本地存储的文件中读取JSON? - How can I read JSON from a file stored locally? 如何从 JavaScript 中的 Razor 模型对象获取 JSON 对象 - How to get JSON object from Razor Model object in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM