3

我的应用程序位于服务器上一个单独的虚拟目录下。要访问我的 ASP.NET MVC 应用程序,用户必须访问:

http://server-dev/superApp

我对忽略“superApp”目录部分的 Ajax/Json 服务器请求有疑问。每当发出 Ajax 请求时,Fiddler 都会显示 404,因为不是http://server-dev/superApp/User/GetUsersJson例如,而是http://server-dev/User/GetUsersJson被调用(注意缺少的 superApp 名称)。

Ajax 请求示例:

function GetUsers(id) {
    $.ajax({
        url: "/User/GetUsersJson/",
        data:{ id: id},
        datatype: 'json',
        type:'post',
        success: function (result) {
            ////Do stuff with returned result
        }        
    });      
}

注册路线:

r.Match("User/GetUsersJson", "User", "GetUsersJson");

我应该在哪里查看以及可以更改哪些内容以确保我的应用程序虚拟文件夹始终包含在所有 URL 请求中?

ps 请注意,所有 Javascript/Ajax 逻辑都保存在单独的 .js 文件中,因此没有 RAZOR 语法可用。

4

3 回答 3

4

您是否尝试过使用 HTML 辅助方法?

url: "@Url.ACtion("GetUsersJson","User)"

EDIT : As per the comment

You may get the Path name using the HTML Helper method and Keep that in a Global variable and access that in the external javascript file

In the view

<script type="text/javascript>
  var globalGetJSONPath='@Url.ACtion("GetUsersJson","User)';
</script>

And now you can use it in the external file like this

 $.ajax({
        url: globalGetJSONPath,
        data:{ id: id},
        //remaining items....

  });
于 2012-07-24T18:39:23.110 回答
1

I solved this stuff by passing variable to js that contains hostname+vdir. Because of heavy js url generation.
In other cases Shyju's answer is best way to solve this.

于 2012-07-24T18:40:48.330 回答
1

No way to do it without some server-side code generation. Easiest thing would be defining global variable (sorry) holding you application root and initializing it somewhere in master page.

Javascript generation of route urls always was one of the messiest parts of asp.net mvc.

于 2012-07-24T18:43:08.553 回答