我試圖讓一個控制檯應用程序向ASP.NET MVC 3中開發的Web應用程序發送XML文件,並接收另一個XML作爲響應。如何從控制檯應用程序上傳文件到ASP.NET MVC
在控制檯應用程序返回的錯誤是:
The remote server returned an error: (500) Internal Server Error.
當我Fiddler2運行,我看到這個錯誤:
Object reference not set to an instance of an object.
在控制檯應用程序中的代碼是:
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();
}
MVC控制器中的代碼是:
[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");
}
RunTest()
工作的很好,因爲這種方法工作時,我通過窗體上傳文件(在一個具有相同名稱的方法,使用方法GET)。 RunTest()
返回帶有響應的XML。
當我調試MVC應用程序時,我看到問題:變量file
爲空!
我該如何解決這個問題?我在控制檯應用程序中需要更改哪些內容才能真正發送文件?或者是否改變MVC方法?
而且,在嘗試使用WebClient
之前,我在此嘗試了此代碼:http://msdn.microsoft.com/en-us/library/debx8sh9.aspx,並得到了相同的結果。
它的工作!我只需要刪除'作爲HttpPostedFile',它不是編譯...非常感謝! – 2012-04-03 22:40:59
感謝隊友,幫助我! – f0x 2012-04-24 21:13:25