繁体   English   中英

如何在 Web 中调用控制台应用程序功能 forms 在 asp.net 中

[英]How to call Console Application functions in Web forms in asp.net

我有一个包含两个项目的解决方案,即 web 应用程序项目和控制台应用程序。

I call web API in the console application (in Program.cs class) and store the output in a list, and I want to display the list in my web form in the web application project. 如何从 Program.cs 调用 function 到我的网络表单以显示结果?

或者如果有一个最简单的解决方案而不是这个?

我假设您从控制台应用程序调用的 Web API 是外部应用程序(不是您的 web 应用程序的一部分)。

我会在一个单独的项目中将逻辑读数与 Web API 分开,并从控制台和 web 应用程序中使用它。

您可以参考以下步骤调用web forms中的控制台应用程序功能。

首先,请在您的控制台应用程序中定义 program.cs。

namespace TestConsole
{
    public class Program
    {
        static void Main(string[] args)
        {

        }

        public static List<string> Liststring()
        {
            List<string> list = new List<string>();
            list.Add("test1");
            list.Add("test2");
            list.Add("test3");
            list.Add("test4");
            return list;

        }
    }
}

二、在web app中,请点击add-reference ->Projects->选择console app。

第三,请在代码顶部添加 using 语句。

最后,您可以使用以下代码在下拉列表中填写一个列表。

using System;
using System.Web.UI;
using TestConsole;


namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Dropdownlist1.DataSource = Program.Liststring();
                Dropdownlist1.DataBind();
            }
      

        }
    }
}

结果:

在此处输入图像描述

暂无
暂无

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

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