views:

75

answers:

2

I'm trying to include script and style references that will not break on deployment, however I can not even get the references to work locally. I have tried using Url.Content() and MVCContrib's <%=Html.ScriptInclude("")%>.

My scripts are in a Scripts folder on the root of the site; my styles are in the usual Content/css/ folder.

The scripts render like this:

<script type="text/javascript" src="/Scripts/MicrosoftAjax.debug.js" ></script>

This will not work in a view page in the Views folder. What am I doing wrong and what is the best way to handle this?

I would have thought Url.Content() would at least work for styles but used in my master page, the link rendered

<link href="/Content/css/Site.css rel="stylesheet" type="text/css" /> 

This does not work, because the Master Page is in a Shared folder, so what is really the way forward with this?

A: 

I do it like this:

<link href="<%= ResolveUrl("~/Content/css/Site.css")%>" rel="stylesheet" type="text/css" />

Also, take the runat=server out of your head tag if you don't want it trying to do you favors.

(Edit - fixed a misplaced quote)

Update: To clarify, I do in fact have this working in development (localhost:1227, root), on my test server (full domain, root) and in production (different domain, in a subdirectory). Works great in every case!

Chris
+2  A: 

<link href="<%= ResolveUrl("~/Content/css/Site.css")%>" rel="stylesheet" type="text/css" />

works for your style sheet. but if you are on MVC2 and you have the files in the script dir then you can use the new helper:

<%=Html.Script("scriptfile.js") %>

this is better practice as you can also specify a file for release and debug mode:

<%=Html.Script("scriptfile-min.js", "scriptfile.js") %>
Richard
+1 - Didn't know about that!
Chris