处理 ASP.NET 和母版页中的相对文件路径
这可能是一个非常简单的问题,我会被嘲笑,但我在母版页中使用文件路径时遇到困难。 我相信这是因为如果子目录中的页面使用母版页,则文件路径不正确。
为了解决这个问题,我需要从根获取文件路径,但我似乎无法让它工作。
我尝试过:
<script type="text/javascript" src="~/jQueryScripts/jquery.js"></script>
但
<script type="text/javascript" src="../jQueryScripts/jquery.js"></script>
都没有运气!
关于如何告诉它从根获取文件路径有什么想法吗?
This may be a painfully simply question for which I will be mocked but I am having difficulty in using filepaths in master pages. I believe this is because if a page in a sub-directory to using the master page then the filepath is incorrect.
To fix this I need to get the filepath from the root but I can't seem to get it working.
I tried:
<script type="text/javascript" src="~/jQueryScripts/jquery.js"></script>
and
<script type="text/javascript" src="../jQueryScripts/jquery.js"></script>
No luck on either!
Any ideas on how I can tell it to get the filepath from the root?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我只是假设文件路径,你实际上指的是 url (或 uri,我忘记哪一个是部分的)。
如果没有 ~,第一个示例应该可以工作。
会导致浏览器请求 http://www.example.com/jQueryScripts/jquery.js (其中 www.example.com 是您的域)。
I'm just assuming by filepath, you actually mean url (or uri, I forget which one is partial).
Without the ~, the first example should work.
<script type="text/javascript" src="/jQueryScripts/jquery.js"></script>
would cause the browser to request http://www.example.com/jQueryScripts/jquery.js (where www.example.com is your domain).我相信您需要在
MasterPage
的标记中添加
runat=server
才能使此 URL 变基工作。I believe you need to have
runat=server
in the<head>
tag of theMasterPage
for this URL rebasing to work.首先前面的波浪号是一个用于服务器控件的 asp.net 东西,在基本 HTML 中不起作用。
无需详细解释,您只需在前面使用斜杠 (/),并包含 Web 应用程序名称(如果它不是根站点)。
或者,您可以将代码放入母版页中以动态包含脚本,并让它处理路径。 就像:
上面的代码还附加了汇编版本。 我主要将其用于开发,因此每当我构建时我的 javascript 文件都会更新。
First off the tilde in front is a asp.net thing for use in server controls and won't work in basic HTML.
Without getting into detailed explanations you could just use a slash (/) in front, and include the web app name if its not the root site.
Or you could put code in your master page for dynamically including scripts, and let it handle the pathing. Like:
The above code also appends the assembly version. I use this mostly for development so my javascript files get updated whenever I build.
您可以使用 Page.ResolveUrl 方法来解决此问题,
例如:
You could use the Page.ResolveUrl method to get around this
for example: