title: How to upload a file to ASP.NET MVC from a console application
body: <p>I'm trying to have a console application to send a XML file to a web application developed in ASP.NET MVC 3, and receive another XML as a response. </p>
  
  <p>The error returned in the console application is: </p>
  
  <blockquote>
    <p>The remote server returned an error: (500) Internal Server Error.</p>
  </blockquote>
  
  <p>When I get Fiddler2 running, I see this error:</p>
  
  <blockquote>
    <p>Object reference not set to an instance of an object. </p>
  </blockquote>
  
  <p>The code in the console application is:</p>
  
  <pre><code>static void Main(string[] args)
  {
      var wc = new WebClient();
      byte[] response = wc.UploadFile("http://mysite.com/Tests/Test", "POST", "teste.xml");
      string s = System.Text.Encoding.ASCII.GetString(response);
      Console.WriteLine(s);
      Console.ReadKey();
  }
  </code></pre>
  
  <p>The code in the MVC Controller is:</p>
  
  <pre><code>[HttpPost]
  public ActionResult Test(HttpPostedFileBase file)
  {
      XElement xml = XElement.Load(new System.IO.StreamReader(file.InputStream));
      var test = new MyTest();
      return File(test.RunTest(xml), "text/xml", "testresult.xml");
  }
  </code></pre>
  
  <p><code>RunTest()</code> works well, since this method works when I upload the file via form (in a method with the same name, using method GET). <code>RunTest()</code> returns the XML with the response.</p>
  
  <p>When I debug the MVC application, I see the problem: the variable <code>file</code> is null!</p>
  
  <p>How do I fix that? What do I have to change in my console application for it to actually send a file? Or is it the case to change the MVC method?</p>
  
  <p>And, before trying to use <code>WebClient</code>, I tried this code here: <a href="http://msdn.microsoft.com/en-us/library/debx8sh9.aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/debx8sh9.aspx</a>, and had the same results.</p>