push 22
push 22

Reputation: 1182

asp.net mvc 3: deployment. getJson urls don't work

I have a very simple asp.net mvc3 app that uses jquery::getJSON to call into my controller and get some data top display via jquery::tmpl.

function ajaxError(jqXHR, textStatus, errorThrown) {
    alert(errorThrown);
}
....

    $.ajaxSetup({
        cache: false,
        error: ajaxError // tell me what the error was
    });

    var cl = $("#listcontainer");
    $(cl).empty();
    $.getJSON("/Home/GetSomeData", { oldData: "" }, function (data) {
        $.each(data, function (i, item) {
            var t = $("#listitem").tmpl(item);
            $(cl).append(t);
        });
    });

Everything works ok under IIS express, however When I deploy the app to a freshly setup iis7 on win2k8 r2, the getJSON call fails with the error "Not Found" being displayed via the ajaxError function. (the actual app runs ok otherwise).

I can actually call the action from the browser by typing it in - http://webservername/myapp/Home/GetSomeData - and it returns the json to me.

Is this a configuration error? Or am I not supposed to be doing it like this?

TIA.

Upvotes: 2

Views: 4152

Answers (2)

Jamie Dixon
Jamie Dixon

Reputation: 53991

The issue here is that your URLs are hardcoded and don't contain the virtual directory you're working from.

Rather than hardcode your URLs you should look at using the routing built into MVC.

You can use the Action method of the UrlHelper to generate links for you such as:

Url.Action("GetSomeData","Home")

So that:

$.getJSON(@Url.Action("GetSomeData","Home"),[...]

Upvotes: 3

Chris Snowden
Chris Snowden

Reputation: 5002

Use the UrlHelper like below to generate the correct URL in both cases:

Url.Action("GetSomeData", "Home")

If using razor this would end up like:

$.getJSON("@Url.Action("GetSomeData", "Home")", { oldData: "" }, function (data) {
    $.each(data, function (i, item) {
        var t = $("#listitem").tmpl(item);
        $(cl).append(t);
    });
});

Upvotes: 3

Related Questions