C#如何从控制台应用程序调用MVC Action方法 [英] C# how to call MVC Action method from console application
问题描述
我正在控制台应用程序上工作,该控制台应用程序获取一些数据并调用MVC3 Action方法,并将获取的数据作为参数传递给该Action方法. 但是我的问题是控制台应用程序如何知道数据成功传递\ MVC操作方法调用正确并且在服务器上的mvc应用程序是否正在运行
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
这是我的代码:
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
}
请指导我...
谢谢, 拉吉
推荐答案
您不能像在此那样调用MVC操作,桌面应用程序和Web应用程序是不相关的.它们作为两个不同的实体存在.需要从您的桌面应用程序调用mvc操作,就像使用您的桌面应用程序调用任何其他Web端点一样,您需要创建一个HTTPRequest.
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..
在您的桌面应用程序中,创建一个HTTPRequest,如下所示:
In your desktop application create a HTTPRequest as follows:
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:
用于获取请求
string url = string.Format(
"http://mysite/somepage?key1={0}&key2={1}",
Uri.EscapeDataString("value1"),
Uri.EscapeDataString("value2"));
和 用于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();
这篇关于C#如何从控制台应用程序调用MVC Action方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!