阿贾克斯,asp.net MVC3路线和相对URL [英] Ajax, asp.net mvc3 routes and relative urls

查看:135
本文介绍了阿贾克斯,asp.net MVC3路线和相对URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要发表一个url像这样一个ASP.NET MVC3应用程序:

I have an ASP.NET MVC3 application published to a url like this:

http://servername.com/Applications/ApplicationName/

在我的code,我使用这样的jQuery的Ajax请求:

In my code, I am using jquery ajax requests like this:

$.get(('a/b/c'), function (data) {}, "json");

当我在本地运行的应用程序,Ajax请求直接进入正确的页面(作为一个MVC的路线),因为当地的页面带有/(本地主机/ A / B / C端)。

When I run the application locally, the ajax request goes directly to the correct page (being an mvc route) because the local page ends with a "/" (localhost/a/b/c).

然而,当我发布到 http://servername.com/Applications/ApplicationName/ ,结尾的/并不总是present。该URL可以是 http://servername.com/Applications/ApplicationName ,然后使Ajax请求尝试加载 http://服务器.COM /应用/ ApplicationNamea / B / C ,其失败原因是显而易见的。

However, when I publish to http://servername.com/Applications/ApplicationName/, the trailing "/" is not always present. The url could be http://servername.com/Applications/ApplicationName, which then causes the ajax request to try to load http://servername.com/Applications/ApplicationNamea/b/c, which fails for obvious reasons.

我已经研究过重写URL追加斜线,但A)它没有工作,和B),我觉得这是一个贫穷的解决问题的办法,而且这将是更好配置的javascript本地文件夹设置的URL到正常不管工作。

I have already looked into rewriting the url to append a trailing slash, but A) It didn't work, and B) I feel like it's a poor solution to the problem, and that it would be better to configure the javascript urls to work properly regardless of the local folder setup.

我也尝试../a/b/c和/ A / B / C,但既不似乎工作。

I did try "../a/b/c" and "/a/b/c", but neither seemed to work.

在此先感谢您的帮助!

推荐答案

我个人倾向于在我看来就像使用服务器的相对URL的一个全局变量:

Personally I tend to use a global variable of the relative URL of the server in my view like:

var BASE_URL = '@Url.Content("~/")';

然后,你可以做这样的事情:

Then you can do things like :

$.get(BASE_URL + 'a/b/c'), function (data) {}, "json");

我想补充一点,如果你希望它是完全全局的,你可以把它添加到您的/Views/Shared/_Layout.cshtml代替。

I would like to add that if you want it to be totally global, you could add it to your /Views/Shared/_Layout.cshtml instead.

这篇关于阿贾克斯,asp.net MVC3路线和相对URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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