I'm getting a webpage not available error when trying to access a controller action in an MVC4 Web-API app with VS2010. I am trying to upload a small sized (less than 1MB) pdf document, create a byte[] to pass on to another service. However, I can't get into either my normal controller or my api controller. My app works and all views/partials/etc. show up fine except for this one (the page with the file upload form). This view is a strongly typed partial.
I've tried using the method shown here: Upload a file MVC 4 Web API .NET 4 as well as here: http://blogs.msdn.com/b/henrikn/archive/2012/03/01/file-upload-and-asp-net-web-api.aspx and both of them don't work because my action attribute can't find my action. Whether I put api/Documents or Home/api/Documents it won't work. So i gave up and went back to my html helper beginform, hoping it would find it that way...but it didn't. So after giving up on the fancy web-api stuff (couldn't get async to work), I figured I'd just go old school and pass in the file through a form, but I get the same error. I've also tried re-creating the page, adjusting my httphandlers, runtime adjustments, routes and apiroutes, and am completely at a loss. Please help!
My UI:
My 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 doesn't make sense, but i have a breakpoint to just see if it even gets here, which it doesn't...
[HttpPost]
public AddDocumentResponse AddDocument(HttpPostedFileBase newFile)
{
AddDocumentResponse response = new AddDocumentResponse();
return response;
}
My normal 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"/>