asp.net mvc 3:部署. getJson网址不起作用 [英] asp.net mvc 3: deployment. getJson urls don't work

查看:99
本文介绍了asp.net mvc 3:部署. getJson网址不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的asp.net mvc3应用程序,它使用jquery :: getJSON调用我的控制器,并通过jquery :: tmpl获取一些数据顶部显示.

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);
        });
    });

在IIS Express下一切正常,但是,当我将应用程序部署到win2k8 r2上的新设置iis7时,getJSON调用将失败,并通过ajaxError函数显示错误未找到". (否则,实际的应用程序可以正常运行).

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).

我实际上可以通过在浏览器中键入操作来调用它- http://webservername/myapp/Home/GetSomeData -它将json返回给我.

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.

推荐答案

这里的问题是您的URL是经过硬编码的,并且不包含您正在使用的虚拟目录.

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

您应该使用MVC内置的路由而不是对URL进行硬编码.

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

您可以使用UrlHelperAction方法为您生成链接,例如:

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

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

因此:

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

这篇关于asp.net mvc 3:部署. getJson网址不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆