sam
sam

Reputation: 164

How do I attach a stylesheet?

ok for anyone else experiencing this in future it was a permissions issue with iis i had to set permissions on the whole tree for the style sheet to render

How do I attach a stylesheet? I've tried the usual way:

<LINK REL=StyleSheet HREF="../../Content/Site.css" TYPE="text/css" MEDIA=screen>

with various file paths. I've also tried with vb url.content scripts. For some reason nothing I try is displayed in the browser.

I get various results:

What else could be the problem?

Its attached to the master file, so i dont know what else is wrong here. alt text

Upvotes: 1

Views: 1595

Answers (3)

IrishChieftain
IrishChieftain

Reputation: 15253

"You can also drag the file from Solution Explorer to the head element of the page in Source view, or drag the file from Solution Explorer and drop it anywhere on the page in Design view."

http://msdn.microsoft.com/en-us/library/bb398932.aspx

UPDATE:

Check this post: http://forums.asp.net/p/1469427/3399574.aspx

The syntax provided by Robert should work.

Upvotes: 1

Zhaph - Ben Duguid
Zhaph - Ben Duguid

Reputation: 26956

What you need to remember with an MVC application is that often your URL's will not match up with your actual file system.

Assuming that you have a folder structure set up similar to the defaults:

/Content/
/Content/Site.css
/Views/
/Views/Home/
/Views/Home/index.aspx
/Views/Shared/
/Views/Shared/site.master

When you add the style declaration to your master page, your relative path makes sense:

../../Content/Site.css

Go two folders up from where I am now, and then down into /content to find Site.css.

However, when you're viewing the page in your browser, that relative path is no longer valid:

http://www.example.com/Home/

Where's two folders down from this? Worse still, a deep link:

http://www.example.com/blogs/2010/11/23/My-posting

This will try and look for a folder called Content in the /blogs/2010/ folder.

You have two options:

  1. Use an absolute link: href="/Content/Site.css" this will tell the browser to always start from the root of your site.
  2. Use the application root link: href="~/Content/Site.css" - which, if the link is in a control that is set to runat="server" should be corrected at runtime to point to the application root.

Responding to comment

Ok, but we're getting somewhere now:

Looking at the URL you posted, on your development machine you're running this site as an application under the root? So when you request the site, you're going to: http://localhost/pulse/ ?

So what do you see in your browser if you request http://localhost/pulse/Content/Site.css ?

What does the Routes collection look like in your Global.asax.cs file? Is there something in there that is interfering with the /Content/ folder?

Upvotes: 3

Robert Koritnik
Robert Koritnik

Reputation: 105019

This is Asp.net MVC. Don't use relative file paths because paths are mostly related to routes. Use Url.Content() helper instead.

<link rel="StyleSheet"
      href="<%= Url.Content("~/Content/Site.css") %>"
      type="text/css"
      media="screen" />

Upvotes: 2

Related Questions