Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 在ASP.NETMVC2.0中使用Url.Content_Asp.net Mvc - Fatal编程技术网

Asp.net mvc 在ASP.NETMVC2.0中使用Url.Content

Asp.net mvc 在ASP.NETMVC2.0中使用Url.Content,asp.net-mvc,Asp.net Mvc,我见过很多例子使用Url.Content引用javascript,形成MVC2中的母版页 <script src="<%: Url.Content("~/Scripts/jquery-1.4.1.min.js") %>" type="text/javascript"></script> 但在运行时我失败了 编译错误 描述:编译服务此请求所需的资源时出错。请查看以下特定错误详细信息,并适当修改源代码 编译器错误消息:CS0103:当前上下文中不存在

我见过很多例子使用Url.Content引用javascript,形成MVC2中的母版页

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

但在运行时我失败了

编译错误 描述:编译服务此请求所需的资源时出错。请查看以下特定错误详细信息,并适当修改源代码

编译器错误消息:CS0103:当前上下文中不存在名称“Url”

我还没有找到Url命名空间的声明位置,是否应该使用其他程序集


VS2010、IIS 7、ASP.net MVC 2.0删除了编辑,因为单引号被视为字符文字,所以会导致“文字中的字符太多”错误。最有可能的原因仍然是打字错误

原始帖子(仍然是UrlHelper类):

Url.Content():Url这里是一个助手方法,有点像Html或Ajax助手

在代码中,我认为它的类别是:

System.Web.Mvc.UrlHelper

即,名称空间是System.Web.Mvc

所以很奇怪,如果你真的在使用上面详述的规范,你不能直接使用它。

alex

尝试添加下面的扩展方法,看看它是否对您更有利

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>";
    }
}
公共静态部分类HtmlHelperExtensions
{
公共静态字符串脚本(此HtmlHelper html,字符串路径)
{
var filePath=VirtualPathUtility.ToAbsolute(路径);
HttpContextBase context=html.ViewContext.HttpContext;
//如果文件已经存在,请不要添加该文件
if(context.Items.Contains(filePath))
返回“”;
返回“”;
}
}
用法:

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


我知道它不会直接回答您的问题,但会允许您移动fwd…

确保您的母版页继承System.Web.Mvc.ViewMasterPage

如果删除该行,您的应用程序是否正常运行?认真地说。。没有System.Web.Mvc.UrlHelper程序集,没有这样的名称空间。我没说有。我说过System.Web.Mvc命名空间中有一个名为UrlHelper的类。只是另一个错误:CS1012:同一行中的字符文字太多。。真奇怪。。(抱歉!Url.Content使用单引号是错误的。请参阅以下SO线程:jim,谢谢!这实际上是可以接受的。但我会尝试找出Url.Content不存在的原因???我正在谷歌搜索,找不到它的文档。也许它真的不存在了?