[英]Does ASP.net MVC 4 have some standardized way of referring to relative paths in JavaScript?
I'm working on an ASP.net MVC application. 我正在开发一个ASP.net MVC应用程序。 One thing that I really like about ASP.net MVC is the way that it allows you to refer to relative paths in server-side code.
我真正喜欢ASP.net MVC的一个方面是它允许你引用服务器端代码中的相对路径。 I was wondering if there is some standardized way of doing this in client-side code.
我想知道在客户端代码中是否有一些标准化的方法。
I have a way of doing this. 我有办法做到这一点。 I write the following in my layout page.
我在布局页面中写了以下内容。
<script type="text/javascript">
var ApplicationPath = '@Url.Content("~/")';
</script>
Making this the first script in my layout page, I can refer to relative paths from any .js file using the ApplicationPath
variable. 将此作为布局页面中的第一个脚本,我可以使用
ApplicationPath
变量引用任何.js文件中的相对路径。 This seems to work well, but I'm left wondering if there is some built-in way of working with relative paths in JavaScript. 这似乎运作良好,但我想知道是否有一些内置的方式在JavaScript中使用相对路径。 This works well for me, but someone else might use a different convention.
这对我很有用,但其他人可能会使用不同的约定。
Does ASP.net MVC 4 have some standardized way of referring to relative paths in JavaScript? ASP.net MVC 4是否有一些标准化的方式来引用JavaScript中的相对路径?
I usually do this in my masterpage header: 我通常在我的主页标题中执行此操作:
<script type="text/javscript">
var conf = { baseUrl: '<%=VirtualPathUtility.ToAbsolute("~/")%>' };
</script>
Then, anywhere else in js you can just use: 然后,js中的任何其他地方你可以使用:
var fullUrl = conf.baseUrl + '/somePath';
Also, you can always get the base domain in JS using: 此外,您始终可以使用以下方式在JS中获取基本域:
var baseUrl = window.location.hostname;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.