【问题标题】:Url helper's invisible inside the layout. ASP.NET MVC 3Url 助手在布局中不可见。 ASP.NET MVC 3
【发布时间】:2011-08-24 20:44:59
【问题描述】:

我正在查看Url 助手扩展方法上的article。我尝试关注这篇文章,但结果在布局中没有可见的扩展方法(共享 *.chtml 文件)。

public static class UrlHelperExtensions
{
    public static String Image(this System.Web.Mvc.UrlHelper helper, String fileName)  
    {             
         return helper.Content("~/Content/Images/" + fileName);  
    }
    public static String Stylesheet(this System.Web.Mvc.UrlHelper helper, String fileName)
    {
        return helper.Content("~/Content/Stylesheets/" + fileName);
    }
    public static String Script(this System.Web.Mvc.UrlHelper helper, String fileName)
    {
        return helper.Content("~/Content/Scripts/" + fileName);
    }
}

// inside the layout I tried to use this:
<link href="@Url.Content("Site.css")" rel="stylesheet" type="text/css"/> 

如何使 Url 扩展方法在布局中可见?

谢谢!

【问题讨论】:

  • 你能发布你的代码来展示你的尝试吗?
  • 您的示例代码在布局文件中调用Url.Content。当您将其替换为 Url.Stylesheet() 时会发生什么?你的UrlExtensions 类在哪个命名空间中?

标签: asp.net-mvc-3 url extension-methods helpers


【解决方案1】:

确保您有一个对您正在使用帮助程序的命名空间的引用。

因此,如果您在 MyClassLib.Web 中创建了类 UrlExtensions,请将 @using MyClassLib.Web 添加到 .cshtml 文件的顶部。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-16
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多