首页 > 解决方案 > 我如何在 Asp .Net 网站中执行 C# 代码

问题描述

我是 C# .NET 的新手。我想知道如何在 .aspx 网站 buttonClick 中执行 .cs 程序。

假设我想在 buttonclick 中执行以下代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
class Program
{
    static void Main(string[] args)
    {
        int a = 10;
        int b = 20;
        int c = a + b;

        Console.WriteLine(c);
        Console.ReadKey();

    }
}
}

和按钮单击 -

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{

}
}

标签: c#asp.net

解决方案


Microsoft 不建议从 Web 应用程序调用 .exe,因为出于安全原因,w3wp.exe 在沙盒环境中运行,因此它启动的任何线程/任务/进程都与您自己启动时不同,因此可能无法按预期工作。

可能的,但不推荐。


推荐阅读