asp.net-mvc 从 Razor 声明式视图使用 MVC HtmlHelper 扩展

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4710853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:44:56  来源:igfitidea点击:

Using MVC HtmlHelper extensions from Razor declarative views

asp.net-mvcasp.net-mvc-3razor

提问by Paul Stovell

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

我试图在我的 App_Code 文件夹中为 MVC 3 RTM 项目创建一个 Razor 声明性助手。

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 Htmlproperty, its of type System.Web.WebPages.HtmlHelperrather than System.Web.Mvc.HtmlHelper.

我遇到的问题是 MVC HtmlHelper 扩展(如 ActionLink)不可用。这是因为编译的帮助程序从 派生System.Web.WebPages.HelperPage,虽然它公开了一个Html属性,但它的类型System.Web.WebPages.HtmlHelper而不是System.Web.Mvc.HtmlHelper.

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

我遇到的错误类型的一个例子是:

'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?)

“System.Web.Mvc.HtmlHelper”不包含“ActionLink”的定义,并且找不到接受“System.Web.Mvc.HtmlHelper”类型的第一个参数的扩展方法“ActionLink”(您是否缺少 using 指令或汇编参考?)

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

我唯一的解决方案是创建我自己的 HelperPage 并覆盖 Html 属性:

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")
}

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

在 MVC 3 中是否意味着如此困难,还是我做错了什么?

采纳答案by Omar

Take a look at Marcind's answerto this question. What you're experiencing is a limitation of putting declarative views in the App_Codefolder.

看看这个问题Marcind的回答。您遇到的是在App_Code文件夹中放置声明性视图的限制。

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)

将你的助手放在 App_Code 中是可行的,但有一些限制会影响某些 MVC 场景(例如:无法访问标准的 MVC Html。助手)

回答by Jake Hoffner

I created an extension method for the WebPages helper so that I can get access to the page helper.

我为 WebPages 助手创建了一个扩展方法,以便我可以访问页面助手。

public static HtmlHelper GetPageHelper(this System.Web.WebPages.Html.HtmlHelper html)
{
 return ((System.Web.Mvc.WebViewPage) WebPageContext.Current.Page).Html;
}

回答by Andrew Stanton-Nurse

Omar's got the right answer here, but I wanted to add something (do feel free to mark Omar's response as the answer).

奥马尔在这里得到了正确的答案,但我想补充一些东西(请随意将奥马尔的回答标记为答案)。

We were aware of this in v1 and weren't able to get a great fix in the product, but David Ebbo (an architect on the ASP.Net team) posted a sample of a Visual Studio Code Generator that is basically a first exploration of the kind of ideas we're looking at to make this work properly: http://blogs.msdn.com/b/davidebb/archive/2010/10/27/turn-your-razor-helpers-into-reusable-libraries.aspx

我们在 v1 中意识到了这一点并且无法在产品中得到很好的修复,但是 David Ebbo(ASP.Net 团队的架构师)发布了一个 Visual Studio 代码生成器示例,这基本上是对我们正在考虑使这项工作正常进行的想法:http: //blogs.msdn.com/b/davidebb/archive/2010/10/27/turn-your-razor-helpers-into-reusable-libraries .aspx

Try that out and see what you think! Let David know if you have comments by posting on his blog.

试试看,看看你的想法!通过在他的博客上发表评论,让 David 知道您是否有任何意见。

回答by Greg Gum

Similar to @Jakes answer:

类似于@Jakes 的回答:

public static class MvcIntrinsics {
    public static System.Web.Mvc.HtmlHelper Html {
        get { return ((System.Web.Mvc.WebViewPage)WebPageContext.Current.Page).Html; }
    }

    public static System.Web.Mvc.AjaxHelper Ajax {
        get { return ((System.Web.Mvc.WebViewPage)WebPageContext.Current.Page).Ajax; }
    }

    public static System.Web.Mvc.UrlHelper Url {
        get { return ((System.Web.Mvc.WebViewPage)WebPageContext.Current.Page).Url; }
    }

}

Usage:

用法:

@MvcIntrinsics.Html.Raw("test")

Source: Dino Esposito - Programming Microsoft ASP.NET MVC

资料来源:Dino Esposito - 编程 Microsoft ASP.NET MVC

回答by Alex

An alternative solution:

另一种解决方案:

Add this on top of your razor-helper file:

将此添加到您的 razor-helper 文件之上:

@functions {
    public static System.Web.Mvc.HtmlHelper<object> HHtml = ((System.Web.Mvc.WebViewPage)WebPageContext.Current.Page).Html;
}

then call it like this:

然后像这样调用它:

@HHtml.ActionLink("actionname")

回答by Dejan

My approach to this is to simply pass the page as a parameter to the helper method. So in your example it would be:

我的方法是简单地将页面作为参数传递给辅助方法。因此,在您的示例中,它将是:

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

Then in your Razor view where you need it call it like this:

然后在你需要它的 Razor 视图中,像这样调用它:

@YourHelperFilename.DoSomething(this)

Doing this immediately gives you access to page properties like Htmlor Urlthat you usually have (and through that the HtmlHelperextensions).

立即执行此操作可让您访问像HtmlUrl通常拥有的页面属性(以及通过HtmlHelper扩展)。

As an additional benefit (if you require this), you also get access to instance properties such as the page's ViewData.

作为额外的好处(如果您需要),您还可以访问实例属性,例如页面的ViewData.

回答by HockeyJ

For the benefit of searchers, I got the same error when creating MVC views as part of a class library (for component re-use). The solution, partially alluded to above, was to add the following using statements at the top of the .cshtml file:

为了搜索者的利益,我在创建 MVC 视图作为类库的一部分(用于组件重用)时遇到了同样的错误。上面部分提到的解决方案是在 .cshtml 文件的顶部添加以下 using 语句:

@using System.Web.Mvc
@using System.Web.Mvc.Html

No further work necessary.

不需要进一步的工作。

回答by Omar

回答by Lee Smith

I know that there are some intellisense issues with MVC 3. I think the helpers will still work if you have the namespace set in web.config.

我知道 MVC 3 存在一些智能感知问题。我认为如果您在 web.config 中设置了命名空间,帮助程序仍然可以工作。

MVC 3 RTM has just been realeased are you using this or a beta?

MVC 3 RTM 刚刚发布,您是使用这个还是测试版?