punkouter
punkouter

Reputation: 5356

AJAX getJSON call works on IIS express but not on IIS (ASP.NET/MVC4)

It finds the MVC controller just find with IIS express. But once I switch to IIS and this call is made it cannot find the resource (404)

Is '/api/Values/5' correct? OR must it be done differently to work in IIS and IIS express ?

If I run in IIS and I type in to the URL

http://localhost/AGS.Web/api/values/5

It finds it with no problem

var RefreshInstructions = function () {
    var SelectedTaskValue = $("#SelectedTaskType_Id").val();
    // Send an AJAX request
    $.getJSON("/api/Values/5", function (data) {
        $('#divTaskInstructions').html(data);
    });
}

Key Value
Request GET /api/Values/5 HTTP/1.1

EDIT 1: it is not reffered to as AGS.Web when I use IIS EXPRESS so I am hoping there is a generic way to refer to the URL for both cases?

EDIT 2: Navigating to http://localhost/AGS.Web/api/values/5 works find but I guess the problem is 'AGS.Web' does not existing in then AJAX call.. So should I prepend some sort of server variable something like HttpContext.Current.Request.Url; ?

Upvotes: 1

Views: 2367

Answers (2)

Greg Mason
Greg Mason

Reputation: 803

If you deploy your application to IIS your URI must include your application name as well. So, as your application name is AGS.Web, then your URI must be http://localhost/AGS.Web/api/valuues/5.

You can automatically detect your baseUri and have it prepend by adding a line in your master page or shared _Layout.cshtml:

    <script type="text/javascript">
        var config = {
            contextPath: '@Url.Content("~")'
        }
    </script>

Then in your JS you prepend by:

var baseUri = config.contextPath;

$.getJSON(baseUri + "api/values/5", function (data) {
    $('#divTaskInstructions').html(data);
});

I got this from a discussion javascript-in-virtual-directory-unaware-of-virtual-directory that I ran across while I was having the same issue during a deployment this week.

Upvotes: 4

Sam Aleksov
Sam Aleksov

Reputation: 1201

There is no difference between IIS Express and IIS from this point of view. Default IIS configuration responds to the GET verb, so you've probably configured your web application to be activated on a different URL or even limited to certain hostnames.

Upvotes: 0

Related Questions