4

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:

form with url My Error: 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"/>
Community
  • 1
  • 1
Beckyjoon
  • 251
  • 1
  • 3
  • 12

3 Answers3

0

I have the same error sometimes because of the Fiddler installed. When it is configured to start as proxy it makes changes to you LAN configuration so it works as proxy and sometimes the system won't work without Fiddler. Hope this helps.

Lanayx
  • 2,731
  • 1
  • 23
  • 35
0

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 your URL want to go instead of "Documents"

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

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

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

In your error page see URL as /Index/AddDocument....which is going wrong way.... so check URL is going correctly or not and GIVE PROPERLY NAME OF CONTROLLER AND ACTION AS DEFINE IN ROUTE FILES....

Prasad Phule
  • 468
  • 4
  • 20
0

For me, the problem was with nginx:

http {
    client_max_body_size 0;
}

I set client_max_body_size to 0. the default was 1M.

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

Community
  • 1
  • 1
Dherik
  • 17,757
  • 11
  • 115
  • 164