jQuery Ajax:从应用根目录引用 MVC 控制器 url
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7713859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery Ajax: Reference MVC controller url from App root
提问by MattW
I have an ASP.NET MVC web application running from http://localhost/myappname
. From jQuery, I make jQuery $.ajax() calls to return partial views based on some user action. I usually call this from a view in the same controller that contains the function I am calling via Ajax. For example, a view in my Home controller has the following function (which works well):
我有一个从http://localhost/myappname
. 在 jQuery 中,我根据某些用户操作进行 jQuery $.ajax() 调用以返回部分视图。我通常从包含我通过 Ajax 调用的函数的同一个控制器中的视图调用它。例如,我的 Home 控制器中的视图具有以下功能(效果很好):
function loadMyPartialView() {
$.ajax({
type: 'GET',
url: 'Home/GetNavigationTreePV/',
success: function (data) { onSuccess(data); },
error: function (errorData) { onError(errorData); }
});
return true;
}
This above url gets resolved to http://localhost/myappname/Home/GetNavigationTreePV
and the partial view is returned correctly.
上面的 url 被解析http://localhost/myappname/Home/GetNavigationTreePV
并正确返回部分视图。
Now, I am trying to use qUint to unit test my functions. In this test case, I just want to verify I get to the end of the function and return true. I have created a QUNIT
controller and corresponding view (which loads my unit test JavaScript file). From the test.js file that contains my unit tests, I try to make calls to the same functions that are in my Home views, like the one above. But, since I am now running out of the QUNIT
controller, the url gets resolved to http://localhost/myappname/qunit/Home/GetNavigationTreePV
.
现在,我正在尝试使用 qUint 对我的函数进行单元测试。在这个测试用例中,我只想验证我到达了函数的末尾并返回 true。我创建了一个QUNIT
控制器和相应的视图(加载我的单元测试 JavaScript 文件)。从包含我的单元测试的 test.js 文件中,我尝试调用与我的主页视图中相同的函数,就像上面的函数一样。但是,由于我现在用完了QUNIT
控制器,所以 url 被解析为http://localhost/myappname/qunit/Home/GetNavigationTreePV
.
I have tried changing the url of my ajax request to /Home/GetNavigationTreePV/
(with the preceding forward slash), but when I do that I get the following url http://localhost/myappname/Home/GetNavigationTreePV
.
我尝试将我的 ajax 请求的 url 更改为/Home/GetNavigationTreePV/
(使用前面的正斜杠),但是当我这样做时,我得到了以下 url http://localhost/myappname/Home/GetNavigationTreePV
。
So, to be clear, I am trying to write my ajax request in a way that will always start from the root of my MVC application, and then append the url parameter given in my $.ajax() function.
所以,要清楚的是,我试图以一种总是从我的 MVC 应用程序的根开始的方式编写我的 ajax 请求,然后附加在我的 $.ajax() 函数中给出的 url 参数。
Is there an easy way to do that? Am I going about this in a weird way?
有没有简单的方法来做到这一点?我会以一种奇怪的方式处理这个问题吗?
回答by Steve
I think in your MVC View page you need @Url.Action
我认为在您的 MVC 视图页面中您需要@Url.Action
function loadMyPartialView() {
$.ajax({
type: 'GET',
url: '@Url.Action("GetNavigationTreePV", "Home")',
success: function (data) { onSuccess(data); },
error: function (errorData) { onError(errorData); }
});
return true;
}
Alternatively you can use @Url.Content
或者你可以使用@Url.Content
function loadMyPartialView() {
$.ajax({
type: 'GET',
url: '@Url.Content("~/home/GetNavigationTreePV")',
success: function (data) { onSuccess(data); },
error: function (errorData) { onError(errorData); }
});
return true;
}
If this is in a .js file, you can pass in the Url like
如果这是在 .js 文件中,您可以像这样传入 Url
loadMyPartialView('@Url.Action("GetNavigationTreePV", "Home")')
loadMyPartialView('@Url.Action("GetNavigationTreePV", "Home")')
回答by MattW
I was able to accomplish this with straight JavaScript using window.location.pathname
. See the following example:
我能够使用window.location.pathname
. 请参阅以下示例:
function loadMyPartialView() {
$.ajax({
type: 'GET',
url: window.location.pathname + 'Home/GetNavigationTreePV/',
success: function (data) { onSuccess(data); },
error: function (errorData) { onError(errorData); }
});
return true;
}