1

我有一个需要在多个视图中使用的 Helper 方法。我尝试使用以下信息:http ://weblogs.asp.net/scottgu/archive/2011/05/12/asp-net-mvc-3-and-the-helper-syntax-within-razor.aspx 。我在 /App_Code/Helpers.cshtml 中定义了这个 Helper 方法。我的观点位于

/resources/_Layout.cshtml
/Views/Index.cshtml
/Views/Home/Index.cshtml

如果我在 _Layout.cshtml 中引用我的 Helper 函数,一切正常。但是,如果我在其他视图中引用我的 Helper 函数,则会出现运行时错误。运行时错误说:

CS0103:当前上下文中不存在名称“MyFunction”

我的 Helper 方法如下所示:

/App_Code/Helpers.cshtml
------------------------
@helper MakeUrlRelative(string url)
{
    @MyNamespace.MyClass.MakeUrlRelative(HttpContext.Current.Request, url)
}

当我在 /Views/Index.cshtml 中引用它时,我会这样使用它:

<link rel="stylesheet" href="@MakeUrlRelative("/resources/css/app.css")" />

当我在 /Views/Home/Index.cshtml 中引用它时

<a href="@MakeUrlRelative("/home/page2")">click me</a>

如您所见,我以相同的方式引用 MakeUrlRelative 方法。就像无法从 /Views/Home/Index.cshtml 访问 Helpers.cshtml 文件一样。什么会导致这个?

4

0 回答 0