7

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

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

0

3 Answers 3

10

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..

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:

For Get Request

string url = string.Format(
            "http://mysite/somepage?key1={0}&key2={1}",
            Uri.EscapeDataString("value1"),
            Uri.EscapeDataString("value2"));

and For POST REQUEST

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();
Sign up to request clarification or add additional context in comments.

4 Comments

ok i got it but how can i pass my (Data) object parameter to mvc action method using above code ie httpwebrequest
@Baz1nga I guess requestWriter.Write() has parameters?
@BhushanFirake next time I will ensure I compile my code, ensure it is running and have a sample application too.
I believe, the answer is still not updated. Data variable is never used. It should be requestWriter.Write(data)
0

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

Comments

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

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.

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.