I have an MVC4 project that I am trying to create a helper for. I have added a folder called "App_Code", and in that folder I added a file called MyHelpers.cshtml. Here are the entire contents of that file:
我有一个MVC4项目,我正在尝试创建一个帮助器。我添加了一个名为“App_Code”的文件夹,在该文件夹中我添加了一个名为MyHelpers.cshtml的文件。以下是该文件的全部内容:
@helper MakeButton(string linkText, string actionName, string controllerName, string iconName, string classes) {
<a href='@Url.Action(linkText,actionName,controllerName)' class="btn @classes">Primary link</a>
}
(I know there are some unused params, I'll get to those later after I get this fixed)
(我知道有一些未使用的参数,我会在我修复之后再找到它们)
I "cleaned" and built the solution, no errors.
我“清理”并构建解决方案,没有错误。
In the page that uses the helper, I added this code.
在使用帮助程序的页面中,我添加了此代码。
@MyHelpers.MakeButton("Back","CreateOffer","Merchant","","btn-primary")
When I attempt to run the project, I get the following error:
当我尝试运行该项目时,我收到以下错误:
Compiler Error Message: CS0103: The name 'Url' does not exist in the current context
编译器错误消息:CS0103:当前上下文中不存在名称“Url”
I can't seem to find the correct way to write this - what am I doing wrong? It seems to be correct as compared to examples I've seen on the web?
我似乎找不到写这个的正确方法 - 我做错了什么?与我在网上看到的例子相比,它似乎是正确的吗?
2 个解决方案
#1
4
As JeffB's link suggests, your helper file doesn't have access to the UrlHelper object.
正如JeffB的链接所示,您的帮助文件无法访问UrlHelper对象。
This is an example fix:
这是一个示例修复:
@helper MakeButton(string linkText, string actionName,
string controllerName, string iconName, string classes) {
System.Web.Mvc.UrlHelper urlHelper =
new System.Web.Mvc.UrlHelper(Request.RequestContext);
<a href='@urlHelper.Action(linkText,actionName,controllerName)'
class="btn @classes">Primary link</a>
}
#2
4
For my helpers I create a base class:
对于我的助手,我创建了一个基类:
using System.Web.WebPages;
using System.Web.Mvc;
namespace MyProject
{
public class HelperBase : HelperPage
{
public static new HtmlHelper Html
{
get { return ((WebViewPage)WebPageContext.Current.Page).Html; }
}
public static System.Web.Mvc.UrlHelper Url
{
get { return ((WebViewPage)WebPageContext.Current.Page).Url; }
}
}
}
And then in my helper I do (to use yours as an example):
然后在我的帮助下我做(以你的为例):
@inherits MyProject.HelperBase
@using System.Web.Mvc
@using System.Web.Mvc.Html
@helper MakeButton(string linkText, string actionName, string controllerName, string iconName, string classes) {
<a href='@Html.ActionLink(linkText,actionName,controllerName)' class="btn @classes">Primary link</a>
}
Also, are you sure you didn't mean to use @Html.ActionLink
(via LinkExtensions
) instead of @Url.Action
? The latter doesn't seem to have a linkText, actionName, controllerName
overload, the former does?
另外,你确定你不是故意使用@ Html.ActionLink(通过LinkExtensions)而不是@ Url.Action吗?后者似乎没有linkText,actionName,controllerName重载,前者呢?
#1
4
As JeffB's link suggests, your helper file doesn't have access to the UrlHelper object.
正如JeffB的链接所示,您的帮助文件无法访问UrlHelper对象。
This is an example fix:
这是一个示例修复:
@helper MakeButton(string linkText, string actionName,
string controllerName, string iconName, string classes) {
System.Web.Mvc.UrlHelper urlHelper =
new System.Web.Mvc.UrlHelper(Request.RequestContext);
<a href='@urlHelper.Action(linkText,actionName,controllerName)'
class="btn @classes">Primary link</a>
}
#2
4
For my helpers I create a base class:
对于我的助手,我创建了一个基类:
using System.Web.WebPages;
using System.Web.Mvc;
namespace MyProject
{
public class HelperBase : HelperPage
{
public static new HtmlHelper Html
{
get { return ((WebViewPage)WebPageContext.Current.Page).Html; }
}
public static System.Web.Mvc.UrlHelper Url
{
get { return ((WebViewPage)WebPageContext.Current.Page).Url; }
}
}
}
And then in my helper I do (to use yours as an example):
然后在我的帮助下我做(以你的为例):
@inherits MyProject.HelperBase
@using System.Web.Mvc
@using System.Web.Mvc.Html
@helper MakeButton(string linkText, string actionName, string controllerName, string iconName, string classes) {
<a href='@Html.ActionLink(linkText,actionName,controllerName)' class="btn @classes">Primary link</a>
}
Also, are you sure you didn't mean to use @Html.ActionLink
(via LinkExtensions
) instead of @Url.Action
? The latter doesn't seem to have a linkText, actionName, controllerName
overload, the former does?
另外,你确定你不是故意使用@ Html.ActionLink(通过LinkExtensions)而不是@ Url.Action吗?后者似乎没有linkText,actionName,controllerName重载,前者呢?