C#如何从控制台应用程序调用MVC Action方法

时间:2022-06-14 14:36:38

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 Action方法并将获取的数据作为参数传递给该操作方法。但我的问题是控制台应用程序如何知道数据成功传递\ 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

3 个解决方案

#1


9  

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();

#2


0  

I think you need to use WebClient or HttpWebRequest class to invoke an action method.

我认为您需要使用WebClient或HttpWebRequest类来调用操作方法。

#3


0  

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 * questions about unit testing asp.net mvc.

这是一个大而复杂的主题,并在许多博客和其他有关单元测试asp.net mvc的*问题上进行了讨论。

#1


9  

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();

#2


0  

I think you need to use WebClient or HttpWebRequest class to invoke an action method.

我认为您需要使用WebClient或HttpWebRequest类来调用操作方法。

#3


0  

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 * questions about unit testing asp.net mvc.

这是一个大而复杂的主题,并在许多博客和其他有关单元测试asp.net mvc的*问题上进行了讨论。