4

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?

2
  • Huh, odd... I thought I used Url.Content and it worked for me; at the bare minimum, you can always use ../../ because all of your views are always one folder deep within the views root folder (unless you customized the process). Commented May 11, 2010 at 19:00
  • true how ever if deployed as a virtual directory, as opposed to a website, would this not mess up the paths? there must be some way of managing this. I too am suprised that Url.Content does not work, oh, well! Commented May 11, 2010 at 22:01

2 Answers 2

5

<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") %>
Sign up to request clarification or add additional context in comments.

Comments

0

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!

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.