Reputation: 19821
I've seen a lot of examples using Url.Content to reference javascript, form MasterPages in MVC 2.
<script src="<%: Url.Content("~/Scripts/jquery-1.4.1.min.js") %>" type="text/javascript"></script>
But on runtime I've got failure,
Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0103: The name 'Url' does not exist in the current context.
I haven't find where Url namespace is declared, should additional assemblies be using?
VS2010, IIS 7, ASP.net MVC 2.0
Upvotes: 8
Views: 8738
Reputation: 13581
Removed edit, as single quotes get treated as character literal, so causes 'too many characters in literal' error. The most likely cause is still a typo, IMHO.
ORIGINAL POST (still stands re the UrlHelper class):
Url.Content(): Url here is a helper method, a bit like the Html or Ajax helpers.
In code, I believe its class is:
System.Web.Mvc.UrlHelper
Ie, the namespace is System.Web.Mvc.
So it is very odd that you can't just use it if, that is, you really are using the spec you detailed above.
Upvotes: 1
Reputation: 22485
alex,
try adding the following extension method and see if it get's you any further
public static partial class HtmlHelperExtensions
{
public static string Script(this HtmlHelper html, string path)
{
var filePath = VirtualPathUtility.ToAbsolute(path);
HttpContextBase context = html.ViewContext.HttpContext;
// don't add the file if it's already there
if (context.Items.Contains(filePath))
return "";
return "<script type=\"text/javascript\" src=\"" + filePath + "\"></script>";
}
}
usage:
<%=Html.Script("~/Scripts/jquery-1.4.2.min.js")%>
I know it won't answer your question directly, but will allow you to move fwd...
Upvotes: 2