简体   繁体   English

如何将控制台C#应用程序添加到ASP.NET WebSite?

[英]How do I add a console C# application to a ASP.NET WebSite?

My question is somewhat similar to this thread : How to include another project Console Application exe in an Asp.Net website? 我的问题有点类似于这个主题: 如何在Asp.Net网站中包含另一个项目控制台应用程序exe?

I was however directed to ask a new question since my question wasn't answered and I could not comment on that thread. 然而,我被指示提出一个新问题,因为我的问题没有得到回答,我无法评论该主题。

So, my situation is this: I have a C# application that is compiled to an exe (for now). 所以,我的情况是这样的:我有一个C#应用程序,编译为exe(现在)。 It works fine as a desktop app. 它可以作为桌面应用程序正常工作。 Now I need to host the application on a website such that users can use it via their browsers. 现在我需要在网站上托管应用程序,以便用户可以通过浏览器使用它。

I thought asp.net would be a good place to start and created a WebSite application. 我认为asp.net是一个开始创建WebSite应用程序的好地方。 Now how do I go about referencing the C# application files so that I can call those methods from the new WebSite application? 现在我该如何引用C#应用程序文件,以便可以从新的WebSite应用程序调用这些方法?

I've tried to include post build events on the console app to copy the files to the WebSite folder, but I'm still unable to reference them. 我试图在控制台应用程序中包含post build事件以将文件复制到WebSite文件夹,但我仍然无法引用它们。

Any other suggestions for what I'm trying to do also welcome :) 对我正在尝试做的任何其他建议也欢迎:)

EDIT: I want to the application to be run on the server, the output of which I will then display on the user's browser. 编辑:我想要在服务器上运行应用程序,然后我将在用户的浏览器上显示其输出。

Assuming I understand what you want: 假设我明白你想要什么:

1- You need to transfer your functionality to a Class Library project. 1-您需要将功能转移到Class Library项目。

2- Add Reference to DLL in your website 2-在您的网站中添加对DLL引用

3- Use the functionality as you like. 3-根据需要使用功能。

But, what you can't use is the functionality from assembly file of type .exe directly. 但是,你不能使用的是直接使用.exe类型的程序集文件的功能。

If you do not need to run the console application and just need to used the methods that is defined inside the console application then I would refactor those methods inside a separate class library and then add a reference to the class library in my web application. 如果您不需要运行控制台应用程序并且只需要使用控制台应用程序中定义的方法,那么我将在单独的类库中重构这些方法,然后在我的Web应用程序中添加对类库的引用。 Then build an interface into the webpage that calls the appropriate methods. 然后在调用适当方法的网页中构建一个接口。

Here is a walkthrough from MSDN showing you how to Create a web application project with a class library 以下是MSDN的演练,向您展示如何使用类库创建Web应用程序项目

You should not run the console application on the server as that is BAD! 您不应该在服务器上运行控制台应用程序,因为它很糟糕! On Windows creating a whole new process for every request is not a good idea. 在Windows上为每个请求创建一个全新的流程并不是一个好主意。

Refactor the console application to either take a TextWriter or (better in my opinion, but may take more refactoring) return the result in some logical fashion. 重构控制台应用程序以获取TextWriter或(在我看来更好,但可能需要更多重构)以某种逻辑方式返回结果。

In an extreme case you could get away with just sending in Console.Out to the library and Response.Output. 在一个极端情况下,您可以通过将Console.Out发送到库和Response.Output来逃避。

It seems unlikely you would really just want to do that, but it may be the least thing you can get away with that isn't really bad. 你似乎不太可能真的只想这样做,但它可能是你可以逃脱的最少的东西,这并不是真的很糟糕。

Think along the lines of Console.WriteLine("Hello World") going to: 按照Console.WriteLine("Hello World")思考:

var lib = new MyLib(Console.Out);
lib.DoSomething();

ctor in this case is: 在这种情况下,ctor是:

public MyLib(TextWriter writer)
{
  _writer = writer;
}

where DoSomething() is: DoSomething()在哪里:

public void DoSomething()
{
   _writer.WriteLine("Hello World");
}

On the web you would then output what you need to output and put your output there by first outputting a <pre> tag and then doing this: 在网络上,您将输出您需要输出的内容并将输出放在那里,首先输出<pre>标签,然后执行以下操作:

var lib = new MyLib(Response.Output);
lib.DoSomething();


public string TellMeSomething()
{
  return "Hello World";
}

I almost don't want to recommend this as I think fully refactoring it to something would be better. 我几乎不想推荐这个,因为我认为完全重构它会更好。 So DoSomething becomes TellMeSomething and the implementation something like: 所以DoSomething成为TellMeSomething和实现类似的东西:

public string TellMeSomething()
{
  return "Hello World";
}

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

相关问题 将控制台应用程序转换为ASP.NET C#中的网站 - Convert Console application to Website in ASP.NET C# 如何将HTML图像滑块添加到asp.net C#网站 - How do I add HTML image slider into a asp.net c# Website ASP.NET 5中的C#控制台应用程序 - C# Console Application in ASP.NET 5 如何将帐户添加到网站ASP.NET C# - How to add accounts to a website ASP.NET C# 如何对从C#控制台应用程序使用表单身份验证的ASP.NET WebAPI进行身份验证? - How can I authenticate to an ASP.NET WebAPI that is using Forms Authentication From a C# Console Application? 使用C#控制台应用程序连接到ASP.NET MVC 4网站的内部DB - connect with C# console application to internal DB of asp.net mvc 4 website ASP.NET初学者问题:我如何实现这样的东西是我的ASP.NET C#网站? - ASP.NET beginner question: How do I implement something like this is my ASP.NET C# website? 如何加载网站,然后在 Asp.Net Core (C#) Razor Pages 中添加数据 - How can I load the website and then later add data to it in Asp.Net Core (C#) Razor Pages 如何在ASP.NET C#应用程序中向GridView添加行 - How to add rows to GridView in asp.net c# application 如何在应用程序状态(C#ASP.net MVC)中存储列表〜数组 - How do I store List ~ Array in the Application State (C# ASP.net MVC)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM