Baxter
Baxter

Reputation: 5845

ASP.NET WebForms Web API jQuery - 404 Error

The following is WORKING on my test web server (IIS 7 on Windows 7):

enter image description here

But the exact same code gives me the following 404 error on both production web servers:
(IIS 7 on Windows Server 2008 AND IIS 6 on Windows Server 2003)

enter image description here

[The Code]

Global.asax routing setup:

    protected void Application_Start(object sender, EventArgs e)
    {
        RouteTable.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{deptId}/{member}",
        defaults: new { deptId = RouteParameter.Optional, member = RouteParameter.Optional}           
        );
    }

Page.aspx jQuery statement:

    $.ajax(
    {
        url: "api/Department/" + '<%= Request.QueryString["deptId"] %>',
        contentType: "application/json",
        dataType: "json",
        success: function (data) {
            alert('hello');
        }
    });

DepartmentController.cs GET method:

[HttpGet]
public Department GetDepartment(int deptId)
{
    var deptRepo = new DepartmentRepository();
    return deptRepo.GetDepartment(deptId);
}

I browsed a lot of similar questions on here but didn't find an answer to this.
Why would the same code work on one web server but not on others?
Is there something special I need to configure in my application or IIS?
I am really stuck on this. Any help would be greatly appreciated.

Upvotes: 1

Views: 1932

Answers (2)

Sunil
Sunil

Reputation: 21406

This is a bit late for an answer, but I think this might help others in a similar situation.

Adding another routing rule to your RouteTable should clear out this not found error. This routing rule should start with the first folder name mentioned in your production server path i.e. start with forms and not api when mentioning the second rule. Let the original rule be there since you can have multiple rules in RouteTable. Also, don't change the path in the jQuery you were using originally because 'forms' will be automatically appended when using 'api/...' in the jQuery url.

Make sure you do not give the name of 'DefaultApi' to this new rule since there is already a rule by the name of 'DefaultApi' that will result in compile-time error.

RouteTable.Routes.MapHttpRoute(
    name: "Rule1Api",
    routeTemplate: "forms/api/{controller}/{deptId}/{member}",
    defaults: new { deptId = RouteParameter.Optional, member = RouteParameter.Optional}           
    );

Upvotes: 1

James
James

Reputation: 21

Not sure if you are still having issues with this. I was able to resolve this issue by adding this to my web.config on the IIS Server. This coming from here. The original answer was found from here

I enabled all verbs (verb="*") to the ExtensionlessUrlHandler-Integrated-4.0 handler in my web config.

<system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true" />
        <handlers>
            <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
            <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
        </handlers>

Upvotes: 2

Related Questions