tags:

views:

55

answers:

3

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

+1  A: 

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.

awrigley
Seriously.. there is no System.Web.Mvc.UrlHelper assembly, there is no such namespace.
alexanderb
I didn't say there was. I said there is a class called UrlHelper in the System.Web.Mvc namespace.
awrigley
Just another error: CS1012: Too many characters in character literal, in the same line.. So strange.. (
alexanderb
Sorry! It is wrong to have single quotes with Url.Content. see the following SO thread:
awrigley
http://stackoverflow.com/questions/1629920/where-is-this-too-many-characters-in-string-literal-message-coming-from
awrigley
+1  A: 

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...

jim
jim, thanks! this is actually acceptable.. but I would try to find out why Url.Content just doesn't exist??? I'm googling now, could not find a documenation for it.. maybe it is really gone?
alexanderb
+2  A: 

Make sure your master page inherits System.Web.Mvc.ViewMasterPage

Erik
True! It started to work!
alexanderb