I’m working in an mvc api and a console application with consume that as a service.
The mvc code looks like this:
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
if (file.ContentLength > 0)
{
var fileName = System.IO.Path.GetFileName(file.FileName);
var path = System.IO.Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
file.SaveAs(path);
}
return RedirectToAction("Index");
}
I tried different approaches and I can’t get nothing more than file == null in the mvc side, any help?
If I understand correctly, you are trying to create a web request from within a console application that should trigger this particular method on your controller to fire.
No matter what issues are, this is a proper way to debug it:
1) test the method with your browser. debug / correct until it works correctly.
2) use a http debugger to sniff a correct http request (Fiddler should do the job)
3) use the
WebRequestfrom within the console application and craft its parameters so that you can mimic the set of parameters sniffed with Fidderyou are done.
there’s no way this approach doesn’t work. the application server is unable to tell what client triggers the request. and if a browser request works correctly then for sure you can mimic the same set of parameters with the
WebRequest. And Fiddler (or similar) can help you to find out what particular POST parameters should be.To me – most probably you are:
a) passing the file wrong (wrong encoding, wrong separator between post parameters) (boundary)
b) not setting the request type correctly
Anyway – without the code it’s impossible to say, thus I am giving you an algorithm you can use to fix the issue on your own.