能给一个aspx得到Nodejs返回值的例子?Nodejs hello world也行。

能给一个aspx得到Nodejs返回值的例子?Nodejs hello world也行。

看晕了,是真心菜,大神没别嘲笑哈。。。

7 回复

当然可以!下面是一个简单的例子,展示如何通过ASP.NET Web Forms (ASPx) 获取 Node.js 服务器的返回值。首先,我们将创建一个简单的 Node.js “Hello World” 应用程序,然后使用 ASPX 页面通过 HTTP 请求获取其返回值。

Node.js 示例 - Hello World

  1. 创建 Node.js 服务器

    创建一个新的文件 server.js 并添加以下代码:

    const http = require('http');
    
    const hostname = '127.0.0.1';
    const port = 3000;
    
    const server = http.createServer((req, res) => {
      res.statusCode = 200;
      res.setHeader('Content-Type', 'text/plain');
      res.end('Hello World\n');
    });
    
    server.listen(port, hostname, () => {
      console.log(`Server running at http://${hostname}:${port}/`);
    });
    

    运行这个 Node.js 服务器:

    node server.js
    
  2. 创建 ASP.NET Web Forms 页面

    创建一个新的 ASP.NET Web Forms 项目,并添加一个新的 .aspx 页面(例如 Default.aspx)。

    Default.aspx 中,添加一个按钮和一个用于显示结果的标签:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>
    
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:Button ID="btnFetchData" runat="server" Text="Fetch Data" OnClick="btnFetchData_Click" />
                <br />
                <asp:Label ID="lblResult" runat="server" Text=""></asp:Label>
            </div>
        </form>
    </body>
    </html>
    
  3. 编写后台逻辑

    打开 Default.aspx.cs 文件并添加以下代码来处理按钮点击事件,并从 Node.js 服务器获取数据:

    using System;
    using System.Net.Http;
    using System.Threading.Tasks;
    using System.Web.UI;
    
    namespace WebApplication1
    {
        public partial class Default : Page
        {
            protected async void btnFetchData_Click(object sender, EventArgs e)
            {
                try
                {
                    using (var client = new HttpClient())
                    {
                        var response = await client.GetAsync("http://localhost:3000/");
                        if (response.IsSuccessStatusCode)
                        {
                            var result = await response.Content.ReadAsStringAsync();
                            lblResult.Text = result;
                        }
                        else
                        {
                            lblResult.Text = "Failed to fetch data.";
                        }
                    }
                }
                catch (Exception ex)
                {
                    lblResult.Text = $"Error: {ex.Message}";
                }
            }
        }
    }
    

解释

  • Node.js 部分:创建了一个简单的 HTTP 服务器,当请求到达时,它会返回 “Hello World”。
  • ASP.NET Web Forms 部分:创建了一个页面,包含一个按钮和一个标签。当按钮被点击时,会触发一个异步请求到 Node.js 服务器,并将响应结果显示在标签中。

这样,你就有了一个完整的例子,展示了如何在 ASP.NET Web Forms 页面中获取 Node.js 服务器的返回值。


aspx是什么?

asp.net前台页面啊

是aspx 向nodejs请求数据么? google一个页面,不懂帮顶。。

Using Microsoft’s XMLHTTP Object to Get Data From Other Web Pages http://www.4guysfromrolla.com/webtech/110100-1.shtml

我只知道用 MSXML2.ServerXMLHTTP 了。上面帖子有代码。

用 jsonp 也可以吧

当然可以!我们可以创建一个简单的 Node.js 应用程序来返回 “Hello, World!”,然后通过 ASPX 页面去请求并显示这个结果。

示例 1:Node.js HelloWorld

首先,我们创建一个简单的 Node.js 服务器:

// server.js
const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello, World!\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

运行这个服务器:

node server.js

示例 2:ASPX 获取 Node.js 返回值

接下来,我们在 ASP.NET 中创建一个页面 Default.aspx 来请求并显示 Node.js 的 “Hello, World!” 输出。

<!-- Default.aspx -->
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="lblMessage" runat="server" Text="Loading..."></asp:Label>
        </div>
    </form>
</body>
</html>
// Default.aspx.cs
using System;
using System.Net;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string url = "http://127.0.0.1:3000/";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "GET";

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                string result = reader.ReadToEnd();
                lblMessage.Text = result.Trim();
            }
        }
    }
}

解释

  • Node.js 部分:

    • 创建了一个简单的 HTTP 服务器,监听 3000 端口。
    • 当客户端请求时,服务器返回 "Hello, World!"
  • ASPX 部分:

    • 创建了一个 ASP.NET 页面,名为 Default.aspx
    • Page_Load 方法中,通过 HttpWebRequest 发送 GET 请求到 Node.js 服务器。
    • 将 Node.js 服务器返回的结果展示在页面上。

这样,当用户访问 ASPX 页面时,会从 Node.js 服务器获取数据并显示在页面上。希望这对你有所帮助!

回到顶部