C# – jquery ajax call to JsonResult controller method results in 404 on IIS6

asp.net-mvcciisjqueryjson

I've been pulling my hair out this morning trying to figure this out.

I have a simple jquery json request to a jsonresult action on my controller. When I run this on my local machine (IIS7), it works fine. When I deploy to a dev machine running IIS6, I get a 404 error.

script:

$(function() {
            $('#search').click(function() {
                var zip = $('#zip').val();
                $.ajax({
                    type: "GET",
                    url: "/Customer/GetCityStateFromZip",
                    data: { zipcode: zip },
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(msg) {
                        $("#stateList").setCityState(msg);
                    }
                });
            });
        });

Controller:

public JsonResult GetCityStateFromZip(String zipcode)
        {
            List<CityState> list = new List<CityState>();
            foreach (var item in dt)
            {
                list.Add(new CityState(){City = item.City, StateCode = item.StateCode, StateName = item.StateName});
            }
             return this.Json(list);
        }

Request Data:

GET /Customer/GetCityStateFromZip?zipcode=85215 HTTP/1.1
Host: mydevserver
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6
Accept: application/json, text/javascript, */*
Accept-Language: en-us,es-mx;q=0.7,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
X-Requested-With: XMLHttpRequest
Referer: http://mydevserver/Customer/Entry

Response Data:

HTTP/1.1 404 Not Found
Date: Wed, 30 Jun 2010 18:01:06 GMT
Content-Length: 1635
Content-Type: text/html
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET

On my dev server running IIS6, I have set a wildcard mapping (C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll) and have the "Verify that file exists" unchecked.

All other aspects of my MVC site work just fine. I'm using jquery all over the place (validation, animation, etc) and that's working fine. I just can't seem to get beyond this ajax issue.

Is there some other setting or mapping issue that I need to address on the IIS6 machine? Perhaps IIS6 doesn't know how to route this request?

Best Answer

Alright, I figured it out with the help from another SO post.

The problem was with the URL being passed. It's obviously different from my machine, and the server I deployed to. I'm embarrassed that I didn't think about this.

I changed the $.ajax call from this:

url: "/Customer/GetCityStateFromZip"

To this, which is using Url.Action to the correct full path:

var url = '<%= Url.Action("GetCityStateFromZip","Customer") %>';
url: url 

And all is well.