简体   繁体   English

在ASP.net MVC 2.0中使用Url.Content

[英]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. 我见过很多使用Url.Content引用javascript的例子,在MVC 2中形成MasterPages。

    <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. 编译器错误消息:CS0103:当前上下文中不存在名称“Url”。

I haven't find where Url namespace is declared, should additional assemblies be using? 我还没有找到声明Url名称空间的位置,是否应该使用其他程序集?

VS2010, IIS 7, ASP.net MVC 2.0 VS2010,IIS 7,ASP.net MVC 2.0

确保您的母版页继承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... 我知道它不会直接回答你的问题,但会允许你移动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): ORIGINAL POST(仍然是UrlHelper类):

Url.Content(): Url here is a helper method, a bit like the Html or Ajax helpers. Url.Content():这里的Url是一个帮助方法,有点像Html或Ajax助手。

In code, I believe its class is: 在代码中,我相信它的类是:

System.Web.Mvc.UrlHelper System.Web.Mvc.UrlHelper

Ie, the namespace is System.Web.Mvc. 即,名称空间是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. 因此,如果您真正使用上面详述的规范,那么您不能只使用它是非常奇怪的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM