views:

19

answers:

1

Why do I have to add /Content/ to the uri when using Url.Content? What's the method useful for? :) Am I missing something?

<link rel="stylesheet" type="text/css" href="<%= Url.Content("~/Content/StyleSheets/screen.css") %>" media="screen" />
A: 

According to the docs:

Converts a virtual (relative) path to an application absolute path.

If your content is in the /Content directory, you'll need to include it. Not all content exists in the /Content directory. For instance in the app I'm working on at the moment, javascript lives in /scripts, so we use

Url.Content("~/scripts/xyz.js");

Which translates the virtual path to the application absolute path.

Since the app may be installed at the root of a webserver, or within a directory, you never know if an absolute path is safe. Using this method takes care of this.

Michael Shimmins
The name should have been a bit more generic then, since MVC projects have "Content" folder by default.
jgauffin