通过命令行执行c#方法
我有一个 Web 应用程序,在 DAL 文件中我有一些方法。这些方法通过调用 API 来获取结果。例如:
public string GetUserName(int userID)
{
HttpWebRequest GETRequest = (HttpWebRequest)WebRequest.Create(url);
GETRequest.Method = "GET";
HttpWebResponse GETResponse = (HttpWebResponse)GETRequest.GetResponse();
Stream GETResponseStream = GETResponse.GetResponseStream();
StreamReader srResponse = new StreamReader(GETResponseStream);
return srResponse.ReadToEnd();
}
我的要求是我需要通过命令行执行这些方法并显示结果。我不明白该怎么做;请建议一种继续进行的方法。
I have a web application, and in the DAL file I have some methods. The methods get results by calling an API. For example:
public string GetUserName(int userID)
{
HttpWebRequest GETRequest = (HttpWebRequest)WebRequest.Create(url);
GETRequest.Method = "GET";
HttpWebResponse GETResponse = (HttpWebResponse)GETRequest.GetResponse();
Stream GETResponseStream = GETResponse.GetResponseStream();
StreamReader srResponse = new StreamReader(GETResponseStream);
return srResponse.ReadToEnd();
}
My requirement is that I need to execute these methods via command the line and show the result. I do not understand how to do this; please suggest a way to proceed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,您需要解析命令行参数 。假设我们定义的命令行格式如下:
yourprogram.exe function_name paramValue
,那么我们需要执行以下操作:然后,您将执行
yourprogram.exe GetUserName 5
控制台。Well, you need to parse the command line arguments. Let's say we define our command line format like this:
yourprogram.exe function_name paramValue
, then we need to do the following:then, you would execute
yourprogram.exe GetUserName 5
in the console.我认为没有直接的方法。您必须创建/添加控制台应用程序项目,添加
DAL
的引用并在Main()
中使用类及其方法。I think there is not direct way. You have to create/add console application project, add the reference of
DAL
and use classes and their methods inMain()
.阅读您的评论后,我认为您正在谈论一个命令行实用程序,用户可以在其中输入方法名称来接收输出。如果是这种情况,那么我建议创建一个新的控制台应用程序,解析并检查正确的参数,如果匹配,则向适当的 url(网络服务的 url)发出网络请求,然后从服务器返回的响应流中提取所需的输出。
after reading your comments, I think you are talking about a command line utility where the user could type-in the method name to receive the output. If that's the case then I'd suggest to create a new console application, parse and check for proper arguments, and if it's matched, then make a webrequest to the appropriate url(url of the webservice), and then extract the desired output from the response stream returned by the server.