How to handle relative paths in ASP.NET MVC? - 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") %>" />

Related

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>

Devise Views Not Getting Styled in Rails

I have been working on an application for the Rhode Island State Police. I decided to use the devise gem to handle user auth. After running rails generate devise user. I went to the /sign_up page, and found that it was all there, but just as html. For an example of my problem check out my app
http://systemsgroup2.herokuapp.com/
http://systemsgroup2.herokuapp.com/users/sign_in
Thanks for the help.
Your stylesheets are referenced relative to the current path.
In your layouts (or where ever you include your stylesheets) change the paths from
<link rel="stylesheet" href="assets/skel.css" />
<link rel="stylesheet" href="assets/style.css" />
<link rel="stylesheet" href="assets/style-wide.css" />
to
<link rel="stylesheet" href="/assets/skel.css" />
<link rel="stylesheet" href="/assets/style.css" />
<link rel="stylesheet" href="/assets/style-wide.css" />
Your stylesheets are not loading for the /user directory because instead of looking for assets/stylesheets it is looking for assets/users/stylesheets. To fix this you can use Rails stylesheet helper instead of manually linking to the stylesheets:
<%= stylesheet_link_tag "application" %>

ASP.NET MVC: how to append a token to CSS URL properly?

I can append a token to my script references like this:
<script type="text/javascript" src="/Some.js?<%= Html.GetToken() %>"></script>
... and this works fine. But, if I try to append the same token the same way to a CSS file reference:
<link rel="stylesheet" type="text/css" href="/Some.css?<%= Html.GetToken() %>" />
... the IIS generates the following markup:
<link rel="stylesheet" type="text/css" href="/Some.css?<%= Html.GetToken() %>" />
I know I'm missing something super simple but I cannot see what exactly. Thanks in advance for any help!
This happens if your <head> section has runat="server". If you can live without it (in ASP.NET MVC you usually can) try it this way.
If not, add or modify the CSS link in your view's Page_Load. If that sounds awful to you, then removing runat="server" should be working :)
There a trick you can use for auto versioning.
Check out the post here:
Auto-versioning in ASP.NET MVC for CSS / JS Files?

Can't get CSS loaded in Master page

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"/>

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" /> ?

Resources