File upload form using MVC4 web API: getting error 101 (net :: ERR_CONNECTION_RESET): connection was reset. error

I get a webpage error message when I try to access a controller action in a MVC4 Web-API application with VS2010. I am trying to upload a small size (less than 1 MB) pdf document, create byte [] to switch to another service. However, I canโ€™t get into either my regular controller or my api controller. My application works and all representations / partial / etc. show fine, except for this (page with the file upload form). This representation is strongly typed partial.

I tried using the method shown here: Download the MVC 4 Web API.NET 4 file , and also here: http://blogs.msdn.com/b/henrikn/archive/2012/03/01/file-upload-and- asp-net-web-api.aspx , and both of them do not work because my action attribute cannot find my action. I put api / Documents or Home / api / Documents, this will not work. So I gave up and returned to my beginform html helper, hoping that he would find it that way ... but that is not the case. Therefore, abandoning the fancy web-api stuff (I couldnโ€™t get async to work), I thought that I would just go to the old school and submit the file through the form, but I get the same error. I also tried to recreate the page, configure my httphandlers, runtime settings, routes and apiroutes, and I am completely at a loss. Please, help!

My user interface:

form with url My mistake: Error

My form:

<div class="tab-pane" id="addDoc"> @using (Html.BeginForm("AddDocument", "Documents", FormMethod.Post, new { @class = "form-horizontal", @enctype = "multipart/form-data" })) { <label class="control-label" for="newFile">Upload : </label> <input name="newFile" type="file" /> <input type="submit" value="Submit" class="btn btn-success"/> } </div> 

My API controller: I know this does not make sense, but I have a breakpoint to just see if it is even here, and it is not ...

  [HttpPost] public AddDocumentResponse AddDocument(HttpPostedFileBase newFile) { AddDocumentResponse response = new AddDocumentResponse(); return response; } 

My regular controller Action:

  [HttpPost] public ActionResult AddDocument(HttpPostedFileBase newFile) { return View("DevNotes"); } 

My WebApiConfig:

  public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "Home/api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } 

My RouteConfig:

  public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); routes.MapRoute( name: "Default2", url: "Home/{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } 

Part of my webconfig:

  <httpHandlers> <add path="*.less" verb="GET" type="dotless.Core.LessCssHttpHandler, dotless.Core" /> </httpHandlers> <httpRuntime executionTimeout="99009" maxRequestLength="2097151"/> 
+4
source share
3 answers

I have the same error sometimes due to the installed Fiddler. When it is configured to run as a proxy server, it makes changes to the configuration of your local network, so it works as a proxy server, and sometimes the system will not work without Fiddler. Hope this helps.

0
source

I think .... in

@using (Html.BeginForm (

  "AddDocument", //Action "Documents", // Controller (only controller name not accept in web api route you written constant string as ( Home/api/ then controller name) so write like that...) 

// Here you write where the URL should go, instead of "Documents"

// if you want the web api config ===> to write "Home / api / Documents"

// if you want in Route Config ===> for the 1st "Docunment" for the second "Home / Document"

  FormMethod.Post, new { @class = "form-horizontal", @enctype = "multipart/form-data" })) { } 

On the error page, see the URL as / Index / AddDocument .... which goes wrong. therefore, check the URL correctly or not and GIVE THE MOST NAME OF THE CONTROLLER AND ACTIONS HOW TO DETERMINE IN THE ROUTE FILES ....

0
source

For me, the problem was in nginx:

 http { client_max_body_size 0; } 

I set client_max_body_size to 0. The default value is 1M.

Source: https://serverfault.com/a/401732

0
source

All Articles