从控制台应用程序转换为Web应用程序时处理对控制台的嵌套调用

时间:2012-04-23 19:01:04

标签: asp.net c#-4.0 console-application

我为控制台环境创建了一个.dll,需要将其迁移到Web环境。问题出现在程序的过程中,需要与最终用户进行通信而不会丢失堆栈中的位置。我已经创建了一个小例子来展示我们的问题。

考虑以下主程序。它与嵌套的Foo和FooB基本上会一直询问用户的文本,直到用户给出5个或更多字符长的字符串。当用户给出这样的字符串时,将显示所有失败的尝试(如果有的话)。在网络版中,将有两个文本框...顶部将显示程序编写的内容以及用户的响应内容;底部将是用户回复的地方。

此示例很简单,可以轻松转换为在用户请求之间存储信息的功能。但是,需要迁移的真正程序要复杂得多,并且不能“重写”。简而言之,当我从控制台应用程序迁移到Web应用程序时,我想保持Foo和FooB不变。

(程序应该在VS 2010中编译并运行)。

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

namespace SampleForStackOverFlow
{
class Program
{
    static public String Foo(String InString)
    {
        LinkedList<String> ListOfAttempts = FooB(InString);
        if (ListOfAttempts.Count > 1)
        {
            Console.Write("You fool...you went through the following\n");
            for(LinkedListNode<String> i = ListOfAttempts.First; i!= ListOfAttempts.Last; i=i.Next)
            {
                Console.WriteLine("{0}", i.Value);     
            }
            Console.Write("before giving a string longer than 4 characters\n");
        }
        return (ListOfAttempts.Last.Value);
    }

    static LinkedList<String>FooB(String InString)
    {
        String CurrentString =InString;
        LinkedList<String> ReturnStringList = new LinkedList<String>();
        ReturnStringList.AddLast(CurrentString);
        while(CurrentString.Length <5)
        {
           Console.WriteLine("{0} is too short.",CurrentString);
           Console.WriteLine("Please give a longer string:");
           CurrentString = Console.ReadLine();
           ReturnStringList.AddLast(CurrentString);
        }
        return ReturnStringList;

    }

    static void Main(string[] args)
    {
        String InString;


        Console.WriteLine("Please give your input.");
        InString=Console.ReadLine();
        while(InString != "**")
        {

        String OutString = Foo(InString);
        Console.WriteLine(OutString);
        InString = Console.ReadLine();
        }


    }
}

}

0 个答案:

没有答案