2

If I have this in my Site.Master file:

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

and then go to View Source on page of my site (when deployed to actual server), it renders like this:

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

But if I have a JS file in the Scripts/ folder, it doesn't mimic the same behavior. Where is the magic? Is there some setting somewhere that is causing this?

Thanks,

~ Justin

2 Answers 2

6

I always use the Url helper to make sure.

<link href="<%= Url.Content( "~/content/site.css" ) %>"
      rel="stylesheet"
      type="text/css" />

<script type="text/javascript"
        src="<%= Url.Content( "~/scripts/jquery-1.3.2.min.js" ) %>">
</script>

In fact, I've actually replaced this all by HtmlHelper extensions that do the same thing.

<%= Html.Stylesheet( Url.Content( "~/content/site.css ) ) %>
<%= Html.Javascript( Url.Content( "~/scripts/jquery-1.3.2.min.js" ) ) %>
Sign up to request clarification or add additional context in comments.

1 Comment

Ah. I didn't know about Url.Content. That is useful. But I'd still like to understand why one is rewritten in the page source and the other isn't.
2

The head has the runat="server" option set which means that it performs this "magic" server-side for link references. AFAIK this does not happen for scripts references.

3 Comments

If that were true, since both things are in the HEAD, wouldn't both get rewritten?
You've thought. I haven't dug around enough in the mechanics of but I know for fact this works. Take out the runat="server" from the head and see what happens. My guess is that Microsoft specifically looks for the link tag and not the script tag.

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.