在 asp.net 中包含 .css 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3305189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 22:26:37  来源:igfitidea点击:

Include .css files in asp.net

asp.netcssvisual-studio

提问by but

Since virturl directory name is not fixed, I wrote code below to include .css file in .aspx page now.

由于 virturl 目录名称不是固定的,我现在编写了下面的代码以在 .aspx 页面中包含 .css 文件。

<link rel="Stylesheet" href="<%= ResolveUrl("~/Css/xxx.css") %>" type="text/css" />

The question is, when I use "ResolveUrl" in tag, IDE is always barking about that all CSS classes is undefined.

问题是,当我在标签中使用“ResolveUrl”时,IDE 总是抱怨所有 CSS 类都未定义。

Is there any better way to define .css file including?

有没有更好的方法来定义 .css 文件,包括?

回答by EndangeredMassa

You can do this with html server controls (notice the runat="server") like so:

您可以使用 html 服务器控件执行此操作(请注意runat="server"),如下所示:

<link rel="stylesheet" runat="server" media="screen" href="~/css/styles.css" />

This will still resolve the virtual directory for you. It should also support the css intellisense and warnings on the aspx page.

这仍将为您解析虚拟目录。它还应该支持 aspx 页面上的 css 智能感知和警告。

回答by SLaks

You can include the files statically inside <% if (false) { %>.

您可以将文件静态包含在<% if (false) { %>.

This way, Visual Studio's IntelliSense will see the files, but the ASP.Net runtime will not.
(And the if (false)block should be optimized away by the compiler, so there should be zero performance hit)

这样,Visual Studio 的 IntelliSense 将看到这些文件,但 ASP.Net 运行时不会。
(并且if (false)块应该被编译器优化掉,所以性能应该为零)

回答by BrunoLM

The IDE will not know that you are including that css file, simply because it will generate in runtime, not before it.

IDE 不会知道您正在包含该 css 文件,因为它将在运行时生成,而不是在它之前生成。

If you need the intelisense put it manually and change later when you need.

如果您需要智能感知,请将其手动放置并稍后在需要时进行更改。