在.net中的.aspx页面链接css的最佳方法是什么

Suk*_*van 1 css asp.net

在我的.net应用程序中,我在CSS文件夹中有Styleshet.css.

现在我想在Sample.aspx中链接这个css.

什么是最好的方法

1.

<link href="CSS/StyleSheet.css" rel="Stylesheet" type="text/css" />
                             OR
Run Code Online (Sandbox Code Playgroud)

2.

<link href="<%=ConfigurationManager.AppSettings["ApplicationUrl"].ToString()%>/CSS/StyleSheet.css" rel="Stylesheet" type="text/css" />
Run Code Online (Sandbox Code Playgroud)

在Web.Config中

<appSettings>
    <add key="ApplicationUrl" value="http://localhost/myapp/" />
</appSettings>
Run Code Online (Sandbox Code Playgroud)

Jam*_*rgy 5

asp.net中最好的方法是选项3:

<link href="~/CSS/StyleSheet.css" rel="Stylesheet" type="text/css" />
Run Code Online (Sandbox Code Playgroud)

~/解析到网站根路径.这和"css/...它之间的区别在于它无论你在哪个子文件夹中都能工作.例如,如果你的代码在

/subsection/default.aspx

你的风格在文件夹中 /css

使用链接"css/stylesheet.css"将解析(错误)到"/subsection/css/stylesheet.css"使用"~/css/stylesheet.css"将解析(正确)到"/css/stylesheet.css"

这也与硬路径根目录的不同之处在于,"/css/stylesheet.css"无论站点的虚拟目录配置如何,它都能正常工作.