Asp.net mvc 3 Url帮助器';它在布局中是不可见的。ASP.NETMVC3
我正在查看onAsp.net mvc 3 Url帮助器';它在布局中是不可见的。ASP.NETMVC3,asp.net-mvc-3,url,extension-methods,helpers,Asp.net Mvc 3,Url,Extension Methods,Helpers,我正在查看onUrlhelpers扩展方法。我试着阅读这篇文章,但结果是在布局(共享的*.chtml文件)中看不到任何编写的扩展方法 公共静态类UrlHelperExtensions { 公共静态字符串映像(this System.Web.Mvc.UrlHelper helper,字符串文件名) { 返回helper.Content(“~/Content/Images/”+文件名); } 公共静态字符串样式表(this System.Web.Mvc.UrlHelper
Url
helpers扩展方法。我试着阅读这篇文章,但结果是在布局(共享的*.chtml文件)中看不到任何编写的扩展方法
公共静态类UrlHelperExtensions
{
公共静态字符串映像(this System.Web.Mvc.UrlHelper helper,字符串文件名)
{
返回helper.Content(“~/Content/Images/”+文件名);
}
公共静态字符串样式表(this System.Web.Mvc.UrlHelper helper,字符串文件名)
{
返回helper.Content(“~/Content/Stylesheets/”+文件名);
}
公共静态字符串脚本(this System.Web.Mvc.UrlHelper helper,字符串文件名)
{
返回helper.Content(“~/Content/Scripts/”+文件名);
}
}
//在布局中,我尝试使用以下内容:
如何使Url
扩展方法在布局中可见
谢谢 确保在使用帮助程序的位置有对命名空间的引用
因此,如果您在
MyClassLib.Web
中创建了类UrlExtensions
,请使用MyClassLib.Web将@添加到.cshtml文件的顶部。您可以发布代码来显示您尝试的内容吗?您的示例代码正在布局文件中调用Url.Content
。将其替换为Url.Stylesheet()
时会发生什么情况?您的urlensions
类在哪个名称空间?
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"/>