C# 如何从控制台应用程序调用 MVC Action 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11643490/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# how to call MVC Action method from console application
提问by Raj
I am working on console application which fetch some data and call the MVC3 Action method and pass the fetched data as a parameter to that action method. But my problem is how console application get know that data pass successfully\MVC action method call correctly and on server mvc application is running or not
我正在开发控制台应用程序,它获取一些数据并调用 MVC3 操作方法并将获取的数据作为参数传递给该操作方法。但我的问题是控制台应用程序如何知道数据成功传递\MVC 操作方法调用正确以及服务器 mvc 应用程序是否正在运行
here is my code :
这是我的代码:
public static void Main()
{
// Mvc application object intialization
HomeController object_Mail = new HomeController();
// Mvc action method call
object_Mail.mailgateway(mvcemails); //mvcemails parameter passed to Actionmethod
}
Please guide me...
请指导我...
Thanks, Raj
谢谢,拉吉
采纳答案by Baz1nga
You cant invoke an MVC action like you have done here, a desktop application and web application are unrelated.. They exist as two distinct entities.. Now if you need to call an mvc action from your desktop application it is like calling any other web end point using your desktop application and you need to create a HTTPRequest..
您不能像这里所做的那样调用 MVC 操作,桌面应用程序和 Web 应用程序无关。它们作为两个不同的实体存在。现在,如果您需要从桌面应用程序调用 mvc 操作,就像调用任何其他 Web使用您的桌面应用程序的端点,您需要创建一个 HTTPRequest..
In your desktop application create a HTTPRequest as follows:
在您的桌面应用程序中,创建一个 HTTPRequest,如下所示:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://"+ <your mvc action endpoint> )
request.Method = "GET";
//specify other request properties
try
{
response = (HttpWebResponse)request.GetResponse();
}
if you want to pass some data to your action i.e action parameters you could build your url as follows:
如果您想将一些数据传递给您的操作,即操作参数,您可以按如下方式构建您的 url:
For Get Request
对于获取请求
string url = string.Format(
"http://mysite/somepage?key1={0}&key2={1}",
Uri.EscapeDataString("value1"),
Uri.EscapeDataString("value2"));
and For POST REQUEST
和 POST 请求
webRequest.Method = "POST";
var data=string.Format("key1={0}&key2={1}",Uri.EscapeDataString("value1"),Uri.EscapeDataString("value2")");
StreamWriter requestWriter = new StreamWriter(webRequest.GetRequestStream());
requestWriter.Write();
requestWriter.Close();
回答by Anuraj
I think you need to use WebClient or HttpWebRequest class to invoke an action method.
我认为您需要使用 WebClient 或 HttpWebRequest 类来调用操作方法。
回答by BC.
It's possible to do this in the way you are doing it. However, most web applications have a lot of dependencies that you would need to stub out or mock using a framework like Moq
可以按照您的方式执行此操作。但是,大多数 Web 应用程序都有很多依赖项,您需要使用Moq 之类的框架来存根或模拟这些依赖项
I frequently reference the DLL containing my MVC apps in unit test libraries, which is equivalent to the scenario you are describing. I use Moq to provide a request context and other dependencies that the controllers require, like session state, database repositories, etc.
我经常在单元测试库中引用包含我的 MVC 应用程序的 DLL,这相当于您所描述的场景。我使用 Moq 来提供控制器所需的请求上下文和其他依赖项,例如会话状态、数据库存储库等。
If you use fake dependencies, it's possible that your controller action will be useless. In that case, I would follow the advice of the other answers.
如果您使用假依赖项,您的控制器操作可能会毫无用处。在这种情况下,我会遵循其他答案的建议。
This is a big, complicated topic and is discussed on many blogs and other stackoverflow questions about unit testing asp.net mvc.
这是一个大而复杂的话题,在许多博客和其他有关单元测试 asp.net mvc 的 stackoverflow 问题中都有讨论。

