url.content(...) 像 jquery 或 javascript 中的方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4745940/
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
url.content(...) like method in jquery or javascript?
提问by Muaz Khan
Possible Duplicate:
Url helper in java script
可能的重复:
java 脚本中的 Url 助手
Url.Content(...) asp.net mvc helper method returns equivalent absolute URL. I'm searching for a method in jquery or javascript that works like this...
Url.Content(...) asp.net mvc helper 方法返回等效的绝对 URL。我正在 jquery 或 javascript 中寻找一种像这样工作的方法......
because, I want to separat javascript code into a file (.js) and you know that file doesn't supports Url.Content(...) inside javscript code....
因为,我想将 javascript 代码分离到一个文件 (.js) 中,并且您知道该文件不支持 javascript 代码中的 Url.Content(...) ....
url(...) method of jquery not works like Url.Content()
jquery 的 url(...) 方法不像 Url.Content() 那样工作
Updated: 22 Jan 2011
更新时间:2011 年 1 月 22 日
Hi guys, I've a workaround:
大家好,我有一个解决方法:
In the .cshtml file, I created a ‘GetPath' function that returns absolute path including domain name and can be accessible inside any .js file. Include following code in any ASP.NET MVC view (.cshtml or .aspx or .vbhtml):
在 .cshtml 文件中,我创建了一个“ GetPath”函数,该函数返回包括域名在内的绝对路径,并且可以在任何 .js 文件中访问。在任何 ASP.NET MVC 视图(.cshtml 或 .aspx 或 .vbhtml)中包含以下代码:
<script type="text/javascript">
var fullPath = '@HttpContext.Current.Request.Url.Scheme://@HttpContext.Current.Request.Url.Authority';
function GetPath(url) {
return fullPath + url;
}
</script>
<script src="@Url.Content("~/JavaScriptFile.js")" type="text/javascript"></script>
And the code inside any javascript file.
以及任何 javascript 文件中的代码。
$(function () {
alert(GetPath('/Content/Site.css'));
});
The result is: http://www.yourDomain.com/Content/Site.cssor localhost:1234/Content/Site.css >> Visual Cassini server
结果是:http: //www.yourDomain.com/Content/Site.css或 localhost:1234/Content/Site.css >> Visual Cassini 服务器
You just need to replace @Url.Content("")
with GetPath('')
in any .js file...
你只需要更换@Url.Content("")
与GetPath('')
任何.js文件...
http://muaz-khan.blogspot.com/2012/02/absolute-or-relative-url-issues-and.html
http://muaz-khan.blogspot.com/2012/02/absolute-or-relative-url-issues-and.html
回答by Giorgi
Have a look at ASP.NET MVC JavaScript Routing
回答by jim tollan
Also,
还,
there's a great example here:
这里有一个很好的例子:
do you write your JavaScript in a ASP.NET MVC view ... or in a separate JavaScript file?
您是在 ASP.NET MVC 视图中编写 JavaScript 还是在单独的 JavaScript 文件中?
that uses the data attributes on jquery to store arbitary objects for exactly the purpose that you propose. Check out the accepted answer for the full details.
它使用 jquery 上的数据属性来存储任意对象,完全符合您提出的目的。查看已接受的答案以获取完整详细信息。