Razor:声明性 HTML 帮助程序 [英] Razor: Declarative HTML helpers

查看:32
本文介绍了Razor:声明性 HTML 帮助程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个简单的声明性 html 帮助程序:

I'm trying to write a simple declarative html helper:

@helper Echo(string input) {
    @input
}

如果我将它嵌入到我想使用它的页面中,该助手就可以正常工作.但是如果我将它移动到一个单独的 .cshtml 文件并将该文件放在 ~/Views/Helpers 目录中,我的视图将无法再编译,因为助手是未找到.根据 Scott Gu 的 关于 Razor 的博客文章,它应该可以工作.

The helper works fine if I embed it into the page I want to use it on. But if I move it to a separate .cshtml file and place that file in the ~/Views/Helpers directory, my view can't be compiled anymore because the helper is not found. According to Scott Gu's blog article on Razor it should work.

我做错了什么?

推荐答案

ScottGu 帖子中描述的 ~/Views/Helpers 位置在 MVC 3 RTM 中不起作用.写那篇文章时,这是一个我们折腾的设计理念,但最终我们无法实施.

The ~/Views/Helpers location as describe in that ScottGu post will not work in MVC 3 RTM. At the time that article was written it was a design idea we were tossing around but ultimately we were not able to implement it.

将你的助手放在 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)

这篇关于Razor:声明性 HTML 帮助程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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