2

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>
  • I am definitely running in Integrated mode
  • The application pool is running under an account with access to the folder
  • The application pool is running in .NET 4.0
  • Disabling and re-enabling ASP.NET 4.5 in Windows features and settings
  • aspnet_regiis - i (not supported for IIS 8.5)
  • Tracing the request: (No new information) enter image description here

  • Changing the ExtensionlessUrlHandler-Integrated-4.0 request path to "*"

  • Changing the ExtensionlessUrlHandler-Integrated-4.0 verbs (POST is already included in the list)
jaybeeuu
  • 1,013
  • 10
  • 25
  • try this way: `url: "/Api/GetSomeData"` – Gene R Dec 02 '15 at 17:52
  • I have tried that and it doesn't work either debugging in VS or running from IIS - mind you i wouldn't expect it to as the WebAPI route i have registered requires an action as well as a controller: `routeTemplate: "api/{controller}/{action}"` – jaybeeuu Dec 03 '15 at 09:13
  • check this http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api – Gene R Dec 03 '15 at 09:26
  • Thanks - i am configuring and using my routing according to a combination of the sections entitled HTTP Methods and Routing by action name. As i said in my post this is not a routing problem - routing would have shown up debugging in IIS Express - this is an IIS configuration issue. – jaybeeuu Dec 03 '15 at 09:40

1 Answers1

1

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.

Community
  • 1
  • 1
jaybeeuu
  • 1,013
  • 10
  • 25