MVC3 相对 URL 路径 - Javascript
我遇到相对路径问题,当 Web 应用程序在域的子目录外运行时,路径不正确。例如 http://www.example.com/webapp/
如果我使用 @Url .Content("~/path/to/action")
在页面上就可以了。我什至可以将 @Url.Content("")
嵌入 javascript 脚本中。我想清理页面,我想将 javascript 放入 js 文件中并引用它。现在 @Url.Content
正在 javascript 文件中被调用,它似乎不起作用(可能出于明显的原因)。我该如何解决这个问题?
我查看了
但这似乎不起作用。
I have an issue with relative paths whereby when the web app is running off subdirectory of the domain, the paths are not correct. e.g. http://www.example.com/webapp/
If I use @Url.Content("~/path/to/action")
on the page it is fine. I can even embed the @Url.Content("")
inside the javascript script. I want to clean up the page I wanted to put the javascript inside a js file and reference that. Now that the @Url.Content
is being called inside the javascript file, it doesn't seem to work (probably for obvious reasons). How can I get around this issue?
I had a look at the <base />
but that doesn't seem to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
现在,您已将所有内容移至单独的 js 文件中,该文件将作为静态内容提供,并且不会解析 Razor 语法。
如果你的js中需要相对路径可能会改变,那么你应该在每个页面中包含一个设置路径变量的脚本,并在这个脚本中使用@Url.Content(...),例如,
然后,声明pathToAction var 在你的js文件中,并根据需要使用它。
Now that you moved everything into a separate js file, the file is being served as static content, and the Razor syntax is not being parsed.
If you need relative paths inside of your js which might change, then you should include a script in each page which sets a path var, and use @Url.Content(...) in this script, e.g.,
Then, declare the pathToAction var in your js file, and use it as needed.