使用MVC的HtmlHelper扩展蛏陈述意见 [英] Using MVC HtmlHelper extensions from Razor declarative views

查看:164
本文介绍了使用MVC的HtmlHelper扩展蛏陈述意见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建我的APP_ code文件夹中的剃刀声明帮手的MVC 3 RTM的项目。

I was trying to create a Razor declarative helper in my App_Code folder for an MVC 3 RTM project.

我遇到的问题是,MVC的HtmlHelper扩展,ActionLink的一样,是不可用的。这是因为编译佣工从 System.Web.WebPages.HelperPage 派生,但它暴露了一个 HTML 属性,类型的 System.Web.WebPages.HtmlHelper ,而不是 System.Web.Mvc.HtmlHelper

The problem I ran into was that the MVC HtmlHelper extensions, like ActionLink, aren't available. This is because the compiled helpers derive from System.Web.WebPages.HelperPage, and though it exposes an Html property, its of type System.Web.WebPages.HtmlHelper rather than System.Web.Mvc.HtmlHelper.

样的错误我得到的一个例子是:

An example of the kind of error I was getting is:

System.Web.Mvc.HtmlHelper'不
  包含'ActionLink的的定义
  没有扩展方法'的ActionLink
  接受类型的第一个参数
  System.Web.Mvc.HtmlHelper可能是
  (是否缺少using
  指令或程序集引用?)

'System.Web.Mvc.HtmlHelper' does not contain a definition for 'ActionLink' and no extension method 'ActionLink' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)

我唯一的解决办法是创建自己的HelperPage并覆盖html属性:

My only solution has been to create my own HelperPage and override the Html property:

using System.Web.WebPages;

public class HelperPage : System.Web.WebPages.HelperPage 
{
    // Workaround - exposes the MVC HtmlHelper instead of the normal helper
    public static new HtmlHelper Html
    {
        get { return ((System.Web.Mvc.WebViewPage) WebPageContext.Current.Page).Html; }
    }
}

然后我必须写在每一个帮手的顶部以下内容:

I then have to write the following at the top of every helper:

@inherits FunnelWeb.Web.App_Code.HelperPage
@using System.Web.Mvc
@using System.Web.Mvc.Html

@helper DoSomething()
{
    @Html.ActionLink("Index", "Home")
}

时的意思是这个很难在MVC 3,还是我做错了什么?

Is it meant to be this hard in MVC 3, or am I doing something wrong?

推荐答案

在看看<一个href=\"http://stackoverflow.com/questions/4451287/razor-declarative-html-helpers/4453637#4453637\"><$c$c>Marcind's回答这个问题。什么您遇到是把声明的看法在 APP_ code 文件夹的限制。

Take a look at Marcind's answer to this question. What you're experiencing is a limitation of putting declarative views in the App_Code folder.

把你的佣工APP_ code ++工程
  但具有一定的局限性
  影响某些MVC情景(为
  例如:没有访问标准MVC
  HTML。佣工)

Putting your helpers in App_Code works but has certain limitations that impact certain MVC scenarios (for example: no access to standard MVC Html. helpers)

这篇关于使用MVC的HtmlHelper扩展蛏陈述意见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆