Abstract:
I have an Asp.net MVC project, and a console windows app.
I try to have the console app to post a file to the web project through a [HttpPost] method in a controller.
Both my projects are running locally, each one in its own visual studio instance.
My problem is that my [httppost] method is never called.
Details:
Here is the code in the web project
public class DevController : Controller
{
[HttpPost]
public ActionResult UploadStuff(HttpPostedFileBase file)
{
System.Diagnostics.Debugger.Break(); //This breakpoint is never reached
return View();
}
}
Here is the code in the console app:
static public async void UploadStuff(Stream streamToUploadToWebProject)
{
var content = new MultipartFormDataContent();
content.Add(new StreamContent(streamToUploadToWebProject), "WhateverName");
var httpClient = new HttpClient();
string url = @"http://localhost:12345/dev/UploadStuff";
var response = await httpClient.PostAsync(url, content);
}
Notes:
1) http://localhost:12345/: This is the url that is displayed in browser when I run my web project.
2) The file that is sent is an unstructured blob.
3) Most related posts I have found are about uploading a json file (like this post), or are about posting a file from a web page (in which case there is a also "Get" method returning a view containing a "upload" button); Both cases are not same as mine.
4) [Edit] HttpClient.PostAsync returns a "404" error.
My Question:
What do I do wrong; how should I write the [HttpPost] method, so that it gets called?
In your controller try to use [FromBody] attribute before the parameter.
[HttpPost]
public ActionResult UploadStuff([FromBody]HttpPostedFileBase file)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.