.net – Razor RTM中的声明性助手方法

前端之家收集整理的这篇文章主要介绍了.net – Razor RTM中的声明性助手方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我通过 his blog看到了Scott Guthrie关于助手方法的帖子.

具体来说:

我看到一堆RC版本的MVC 3帖子关于缺少辅助方法…我看到它的语法支持(@helper)突出显示,但我在/Views/Helpers/SomeHelper.cshtml中有这个(定义为局部视图):

  1. @helper SomeHelper(string text)
  2. {
  3. if (text != null)
  4. {
  5. <text>
  6. @text
  7. </text>
  8. }
  9. else
  10. {
  11. <text>
  12. Unknown
  13. </text>
  14. }
  15. }

我这样使用它:

  1. <div>
  2. Helper with Text:
  3. @SomeHelper("This is not null text.")
  4. </div>

但我得到SomeHelper没有定义….所以我把它搞得一团糟?我是否需要做些什么来将这些视图注册为帮助者?

谢谢.

解决方法

我通过在项目中创建一个App_Code文件夹,然后在该文件夹中创建一个Helpers.cshtml文件来完成此操作.

然后,在.cshtml视图中,使用:

  1. @Helpers.SomeHelper("This is not null text.")

这是我发现在整个Web项目中创建共享声明性帮助器方法的唯一方法.如果还有其他人,我想听听他们的意见.

猜你在找的asp.Net相关文章