Using Url.Content in ASP.net MVC 2.0
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 wh开发者_开发知识库ere Url namespace is declared, should additional assemblies be using?
VS2010, IIS 7, ASP.net MVC 2.0
Make sure your master page inherits System.Web.Mvc.ViewMasterPage
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...
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.
精彩评论