Search code examples
jqueryasp.net-web-apiiis-7.5

getJSON Web API works on Development Server but IIS Express 7.5 throws 404 Not found


Using Visual Studio 2012 and its ASP.NET Development Server everything seemed to be running well for my first forays into ASP.NET. My Web API calls using jquery returned data. In deploying to IIS Express 7.5 in order to start learning Authentication/Authorization I am running into a roadblock in my getJSON call:

GET http://localhost/api/OSList 404 (Not Found)

WebApiConfig

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

getJSON

$.getJSON("/api/OSList", null)
    .success(
        function(data) {
            var mappedOs = $.map(data, function(item) {
                return new Os(item.ID, item.Name, item.PatchLevel);
            });
            self.OSs(mappedOs);
        }
    );

This all worked on Development Server... Is it something I have not set up correctly in IIS that would block this?

Edit: Added controller code, that was an obvious miss on my part. This is default dev studio autogenerated.

public class OSListController : ApiController
{
    private readonly TrendDataEntities _db = new TrendDataEntities();

    // GET api/OSList
    public IEnumerable<OS> GetOS()
    {
        return _db.OS.AsEnumerable();
    }
}

And the HomeController uses:

public ActionResult OSList()
{
    ViewBag.Title = "OS List";
    ViewBag.Message = "";
    return View("~/Views/Home/Admin/OSList.cshtml");
}

Solution

  • Visual Studio Development Server runs your application on a specific port so that http://localhost:<port>/api/OSList will work. If you deploy your application to IIS, your application does not run on a dedicated port but 80 (by default) along with other apps. In that case, your URI must include your application name as well. So, if your application name is MyWebApi, then your URI must be http://localhost/mywebapi/api/OSList. getJSON must be something like $.getJSON("/mywebapi/api/OSList", null).