jaybeeuu
jaybeeuu

Reputation: 1123

All POST MVC Requests give 404 error

I have an MVC App which is giving a 404 errors when i use POST requests.

Here's the pertinent bits of code:

I have a WebAPI controller(although this also happens with "pure" MVC controllers) with a single method in it:

public class GetSomeDataController : ApiController
{
    [HttpPost]
    public string GetNumbers([FromBody]string value)
    {
        // Get 10 randomised numbers

        return //The numbers as JSON;
    }
}

In the JavaScript i have an ajax request:

$.ajax({
    type: "POST",
    url: "/Api/GetSomeData/GetNumbers/",
    data: JSON.stringify(requestData),
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    processData: false, // Pass the data in the body of the request rather than in the URI.
})
.done(function (ajaxResult) {
    // handle the results
})
.fail(function (e) {
    // Handle errors
});

When i debug it from within VS 2013 i get my 10 numbers back no problem at all. However when i run the exact same code in IIS 8.5... enter image description here

The page itself is an MVC routed page so routing is working on the server, but it does not Work when I call a controller action from a POST.

The error page reports that the StaticFile handler appears to be trying to handle the request - which of course fails. enter image description here

I am going out of my head trying to figure out what is going on. Does anyone have any ideas?

These are the things i can remember trying:

Adding various things to web.config (No Discernible effect):

<system.web>
    <httpModules>
        <add name="UrlRoutingModule"
            type="System.Web.Routing.UrlRoutingModule,
            System.Web.Routing, Version=3.5.0.0,
            Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    </httpModules>
</system.web>
<system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
</system.webServer>

,

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>

and

<system.webServer>
  <modules>
    <remove name="UrlRoutingModule-4.0" />
    <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
  </modules>
</system.webServer>

Upvotes: 2

Views: 2161

Answers (1)

jaybeeuu
jaybeeuu

Reputation: 1123

The answer turns put to be nothing to do with IIS or routing.

When debugging in Local IIS or running the site from IIS i was running the application as a sub application - i.e. the address to my application wan't http://localhost/ it was Http://localhost/WebScratchPad/.

The AJAX request was aimiing at Http://localhost/Api/GetSomeData/GetNumbers/ because of this line in the $.ajax request:

url: "/Api/GetSomeData/GetNumbers/",

They should have gone to Http://localhost/WebScratchPad/Api/GetSomeData/GetNumbers/

by doing this:

url: "/WebScratchPad/Api/GetSomeData/GetNumbers/",

So much for poking around in the Web.config!

Of course this isn't the entire solution as idealy that would be dynamic - my application shouldn't have to be installed on a sub application of some website served by IIS - so i will pass a prefix in from the configuration or similar to allow the "/WebScratchPad/" portion to be a little dynamic as per this SO answer.

Upvotes: 1

Related Questions