简体   繁体   English

JavaScript 程序如何与 C# 控制台应用程序通信?

[英]How JavaScript Program Can Communicate With C# Console Application?

I'm trying to write a program that is composed of two parts.我正在尝试编写一个由两部分组成的程序。 The logic part that I prefer to write in C# language because it's object oriented and more important it's the language witch I know the best among objective languages, and the graphic part witch I want to write it with Html, Css and JavaScript and again because they're the most familiar graphical languages witch I can use.我更喜欢用 C# 语言编写的逻辑部分,因为它是面向对象的,更重要的是它是我在客观语言中最了解的语言,以及我想用 Html、Css 和 JavaScript 编写的图形部分,因为它们'是我能使用的最熟悉的图形语言。 Now my question is that how can I transfer data between a C# console app and a JavaScript browser app.现在我的问题是如何在 C# 控制台应用程序和 JavaScript 浏览器应用程序之间传输数据。 Any help will be appreciated.任何帮助将不胜感激。

Edit :编辑

I must clarify that I just need C# console app and not a web app or web api, and therefor this part is not a backend project.我必须澄清一下,我只需要 C# 控制台应用程序而不是 Web 应用程序或 Web api,因此这部分不是后端项目。 And I need to run these two program separately and want them to communicate with each other.而且我需要分别运行这两个程序并希望它们相互通信。

What you describe should be a full stack application to be honest老实说,您所描述的应该是一个完整的堆栈应用程序

With ASP.NET you create your C# backend, setup controllers which handle the interaction between backend code and the front end, and then on the front end you can utilize both Razor markup for HTML templating alongside standard Javascript.使用 ASP.NET,您可以创建 C# 后端,设置处理后端代码和前端之间交互的控制器,然后在前端,您可以将 Razor 标记用于 HTML 模板以及标准 Javascript。

With Blazor web apps, you can take it a step further and have your backend code compile to web assembly and execute directly on the client engine, but it also integrates javascript interop as well.使用 Blazor Web 应用程序,您可以更进一步,将后端代码编译为 Web 程序集并直接在客户端引擎上执行,但它还集成了 javascript 互操作。 Just be mindful about whether you want a client side or server side Blazor app, as each do things a certain way and are meant for different use cases请注意您是否需要客户端或服务器端 Blazor 应用程序,因为每个应用程序都以某种方式做事并且适用于不同的用例

What you describe though, sounds more precisely like you want a seperate javascript app, which should be in communication with an ASP.NET Web API .不过,您所描述的听起来更像是您想要一个单独的 javascript 应用程序,它应该与ASP.NET Web API进行通信。 This way you create your backend and api endpoints, which the javascript app can then make requests against.通过这种方式,您可以创建后端和 api 端点,然后 javascript 应用程序可以针对这些端点发出请求。 You can start your research for that with MS's tutorial here您可以在此处使用 MS 的教程开始您的研究

The dotNet command line tools can generate solutions for all of these, an ASP.NET fullstack web app, ASP.NET Web API solution, as well as the various blazor app options. dotNet 命令行工具可以为所有这些生成解决方案,包括 ASP.NET 全栈 Web 应用程序、ASP.NET Web API 解决方案以及各种 blazor 应用程序选项。 And of course, the standard IDE's like Visual Studio and Rider can create these solutions as well当然,像 Visual Studio 和 Rider 这样的标准 IDE 也可以创建这些解决方案

If you're looking to have direct interop, blazor might be what you're looking for.如果您正在寻找直接互操作,那么 blazor 可能就是您正在寻找的东西。

https://docs.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/call-javascript-from-dotnet?view=aspnetcore-6.0 https://docs.microsoft.com/en-us/aspnet/core/blazor/javascript-interoperability/call-javascript-from-dotnet?view=aspnetcore-6.0

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

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