ASP.NET - 要使用的路径来引用.css和.JS [英] ASP.NET - path to use to reference .CSS and .JS
问题描述
我在我的项目的根母版页。我在我的项目和子文件夹中引用该母版页内容页。什么是引用我的.css和.js文件,如果我总是希望他们能够相对于根的正确方法是什么?
I have a Master Page in the root of my project. I have Content Pages throughout my project and in subfolders referencing this Master Page. What is the correct way to reference my .CSS and .JS files if I always want them to be relative to the root?
下面是如何,我现在做的吧:
Here is how I'm doing it now:
link href="/common/css/global.css"
script src="/common/javascript/global.js"
但是,这打破了联系。我想没有前导/,但并没有对我的子文件夹中的网页。
But that breaks the link. I tried without the leading "/" but that didn't work on my pages in the subfolders.
推荐答案
我会使用类似
Server.ResolveClientUrl("~/common/css/global.css")
这将在任何时候会为你适当的URL。
This will get a proper url for you at all times.
例如:
每评论,这将是完整的用法。
Per the comment this would be full usage.
<link type="text/css" rel="stylesheet"
href='<%= Server.ResolveClientUrl("~/common/css/global.css") %>' />
据评论,其他有效的使用,也没有错误CS1061:'System.Web.HttpServerUtility'不包含一个定义错误:
According to comments, other validated usage, no "error CS1061: 'System.Web.HttpServerUtility' does not contain a definition" error:
<script type="text/javascript"
src="<%= Page.ResolveUrl("~/Scripts/YourScript.js") %>" ></script>
同样重要的是要始终把关闭标签。
Also is important to always put the closing tag .
这篇关于ASP.NET - 要使用的路径来引用.css和.JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!