8

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

<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.

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

3 Answers 3

25

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

<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.

Sign up to request clarification or add additional context in comments.

2 Comments

The tilde (~) URL syntax requires runat="server".
The tilde works on the link tag without runat="server" if that is set on the head tag instead, i.e. <head runat="server">. I actually cannot get it to work if it is not set on the head tag and only on the link tag.
3

You can include the files statically inside <% 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)

1 Comment

Arrrr you typed faster than me! This is the best solution IMO. You can continue referencing files the way you are now and then duplicate all of your references using physical paths and wrap the duplicates in an if(false) { }
0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.