Can't get CSS loaded in Master page - asp.net-mvc

So I am trying to simply load a .css file from within the master page.
The master page Admin.Master and the css file AdminView.css are both in the /views/shared folder. I am sticking the following link tag in the section of my Master page.
I have tried both:
<link href="<%: ResolveUrl("AdminView.css")%>" rel="stylesheet" type="text/css"/>
and
<link href="AdminView.css" rel="stylesheet" type="text/css"/>
As well as every other combination (~/views/shared/adminview.css OR /adminview.css....etc)
and when viewing in Firebug what it loads, it always returns "Resource not found".
NOTE: I have double checked the name and spelling.
Its these trival stuff that shouldn't be this difficult.

You can't access resources in the Views folder directly from the web. You should put your content in the Content folder (or Content/Styles) and reference it from there. The Views folder is for the framework to use to find your Views, it isn't visible from the web.
<link href="<%: Url.Content( "~/content/styles/adminview.css" ) %>" ... />

<link href="/views/shared/AdminView.css" rel="stylesheet" type="text/css"/>

Related

CSS link in html

I'm linking to a css file in html but it doesn't seem to apply.
/style.css" rel="stylesheet">
html is complaining on the second < as if it wouldn't be closed and is marked red. Anyone who know's anything about this issue?
The CSS link in an HTML page you have to do it like this
<link rel="stylesheet" href="style.css" /> provided that the HTML and CSS file are in the same folder

MVC head components

I'm quite new to MVC. In my website I use a layout for all the views and I was wondering how could I add links to certain .css files.
What I mean is that if I add them in the _layout.cshtml, all HTML files will have that link although some of them don't need it, which could lead to performance issues??
How could I do this? Thanks!
Layout page works like a master page. You can define a section in the partial view and then render it in the layout page:
Define a section named (Styles) in the view that needs (yourView_style.css) file to be rendered:
#section Styles {
<link href="#Url.Content("~/Styles/yourView_style.css")" rel="stylesheet" type="text/css" />
}
In (_layout.cshtml) render the (Styles) section:
<head>
<link href="#Url.Content("~/Styles/main.css")" rel="stylesheet" type="text/css" />
#RenderSection("Styles", false)
</head>

wicked_pdf does not include styles

I used
<%= wicked_pdf_stylesheet_link_tag "pdf" %>
it shows the following output in the html
<link href="/stylesheets/pdf.css?1302860585" media="screen" rel="stylesheet" type="text/css">
<link href="file:///home/likewise-open/NEXTBRIDGE/nazar.hussain/osd/development/atlantis/public/stylesheets/pdf" media="screen" rel="stylesheet" type="text/css">
But when create the pdf it do not have any style. If i copy all css from the file to the header of the page, it includes all styles. What is the issue and how to solve it.
I personally haven't had any trouble with this method, but I've seen others that have.
I just pushed some updates to the main project that should resolve this issue for you.
wicked_pdf_stylesheet_link_tag now will inline any css files passed in directly into the markup.
I also updated the Github issue you created.

Can't get CSS to load

I can't seem to get a css file to apply its styling to a form. It is quite frustrating now because as far as I know it should work!
In my head I have
<link href="/stylesheets/formtastic.css?1290524609" media="screen" rel="stylesheet" type="text/css" />
and in the body I have:
<form action="/agents" class="formtastic agent" id="new_agent" method="post">
The formtastic.css file should apply styling to the form. It's contents are viewable here:
formtastic.css
Any suggestions or fixes?
The problem was that the following code was not inserted in the form:
<% f.inputs do %>
This creates the html and now the css works.
i.e. the html missing was
dont you need to turn that number into a querysting var?
<link href="/stylesheets/formtastic.css?math=1290524609" media="screen" rel="stylesheet" type="text/css" />
Are you using this with Rails? If so, you could simply put this in your header:
<%= formtastic_stylesheet_link_tag %>
Also, have you run both bundle install and rails generate formtastic:install (for Rails 3, use ./script/generate formtastic for Rails 2)?
Are you sure the path is correct? if you check source with firebug check the link tag and see if its contents are a 404 error. It may be an issue with your root-relative path.
This is only a guess as I can't see how your site is structured.
Are you sure you have to begin with a forward slash ? (/)
Cause it's mean it's at the root of your domain.
Tried <link href="stylesheets/formtastic.css?1290524609" media="screen" rel="stylesheet" type="text/css" /> ?

How to handle relative paths in ASP.NET MVC?

I have a master page which references a style in the following manner:
<link rel="stylesheet" type="text/css" href="../../Content/Style.css" />
All my pages inherit from this master page. And this works well when the URL is http://www.domain.com/home/details/5, however the URL is http://www.domain.com/home/create, then, of course, Style.css cannot be found because `../../Content/Style.css' resolves to a directory one higher where there is nothing there.
How is this typically handled?
Use Url.Content("~/Content/Style.css") to resolve the path safely.
"~" means the host.
e.g.
<link rel="stylesheet" type="text/css"
href="<%= Url.Content("~/Content/Style.css") %>" />

Resources