简体   繁体   English

从我的网站到 .NET (C#) 进行通信的正确方法是什么,反之亦然?

[英]What is a proper way to communicate from my website to the .NET (C#) and vice-versa?

I'm using CefSharp as a webbrowser framework in a Visual Studio C# Form application project.我在 Visual Studio C# 表单应用程序项目中使用 CefSharp 作为网络浏览器框架。 I read in the CefSharp documentation that I can communicate from the .NET to my website using CefSharp Javascript Injection .我在 CefSharp 文档中读到,我可以使用CefSharp Javascript Injection从 .NET 到我的网站进行通信

chromeBrowser.ExecuteScriptAsync("document.body.style.background = 'red';");

This line of code will change the background of my document to red.这行代码会将我的文档的背景更改为红色。

I'm pretty sure this is not a proper way to establish a communication from the .NET to the website.我很确定这不是建立从 .NET 到网站的通信的正确方法。 (I would want for example to send data that the .NET project has to the website so the website can update the MySql database and this seems like a very fragile way to do it) (例如,我想将 .NET 项目拥有的数据发送到网站,以便网站可以更新 MySql 数据库,这似乎是一种非常脆弱的方法)

So I continued to google and I stumbled upon this .所以我继续谷歌,我偶然发现了这一点 It says it它说

allows for communication between JavaScript and .Net.允许在 JavaScript 和 .Net 之间进行通信。

Fair enough.很公平。 I tried to read and understand what's going on but it's quite complex.我试图阅读并理解发生了什么,但它非常复杂。

My question is: Is this the proper way to establish a communication from my website to the .NET project and from the .NET project to my website?我的问题是:这是建立从我的网站到 .NET 项目以及从 .NET 项目到我的网站的通信的正确方法吗? Is there a simpler way?有没有更简单的方法?

The thing you've shown is just a communication between JavaScript and .NET您所展示的只是 JavaScript 和 .NET 之间的通信

To actually communicate with the website you should use:要与网站进行实际交流,您应该使用:

  • Web Sockets: Real-time communication between your server and client. Web Sockets:服务器和客户端之间的实时通信。
  • HTTP: You can use usual HTTP requests in C# to send requests and receive responses. HTTP:您可以在 C# 中使用通常的 HTTP 请求来发送请求和接收响应。
  • JavaScript: Run the JavaScript code that will request stuff with CefSharp tools. JavaScript:运行将使用CefSharp工具请求内容的 JavaScript 代码。

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

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