Using Url.Content in ASP.net MVC 2.0

I have seen many examples using Url.Content to reference javascript, a MasterPages form in MVC 2.

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

But at runtime, I had a crash,

Compilation Error Description: An error occurred while compiling the resource required to service this request. Review the following specific error data and modify the source code accordingly.

Compiler Error Message: CS0103: The name "Url" does not exist in the current context.

I have not found where the Url namespace is declared, should additional assemblies be used?

VS2010, IIS 7, ASP.net MVC 2.0

+7
asp.net-mvc
source share
3 answers

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

+8
source share

Alex

try adding the following extension method and see if it gets you 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 already there if (context.Items.Contains(filePath)) return ""; return "<script type=\"text/javascript\" src=\"" + filePath + "\"></script>"; } } 

using:

 <%=Html.Script("~/Scripts/jquery-1.4.2.min.js")%> 

I know that it will not answer your question directly, but will allow you to move fwd ...

+2
source share

Editing has been removed because single quotes are treated as a character literal, therefore causing "too many characters in a literal" error. The most likely reason is still a typo, IMHO.

ORIGINAL MAIL (UrlHelper class still stands):

Url.Content (): Url here is a helper method, a bit like the Html or Ajax helpers.

In the code, I believe its class is:

System.Web.Mvc.UrlHelper

That is, the namespace is System.Web.Mvc.

So it’s very strange that you cannot just use it if, that is, you really use the specification described in detail above.

+1
source share

All Articles