问题描述
我正在创建一组用于呈现兼容的 Twitter Bootstrap html的助手.如我所见,关于如何将这些方法组合在一起,我有两个选择:
I'm creating a set of helpers for rendering compatible Twitter Bootstrap html. As I see it, I have two options when it comes to how to group these methods together:
- 使用TB扩展HtmlHelper前缀的方法
- 创建一个包含方法的新类
TBootHelper
在第二种情况下,要使TBoot
帮助器可用,开发人员将添加
In the second case, to get the TBoot
helper available, the developer would add
<pages pageBaseType="Twitter.Bootstrap.Mvc.TBootViewPage">
是~/Views/web.config
(由@darin指出)
To it's ~/Views/web.config
(as pointed by @darin)
或在需要时实例化帮助程序
Or instantiate the helper when it's needed
@using Twitter.Bootstrap.Mvc
var TBoot = new TBootHelper<TModel>(Html);
我的问题是,我应该创建一个TBootHelper
类还是仅向HtmlHelper
添加方法?
My question is, Should I create a TBootHelper
class or just add methods to the HtmlHelper
?
推荐答案
我将创建一个自定义TBootHelper
和一个自定义基本视图,所有视图都将继承该自定义基本视图,并具有类型TBootHelper
的属性.
I'd go with creating a custom TBootHelper
and a custom base view that all views will inherit from and which will have a property of type TBootHelper
.
我并没有强迫开发人员将@inherits Twitter.Bootstrap.Mvc.TBootViewPage<TModel>
添加到他要使用此自定义帮助程序的每个Razor模板中,而是将其一劳永逸地添加到~/Views/web.config
文件中:
And instead of forcing the developer to add @inherits Twitter.Bootstrap.Mvc.TBootViewPage<TModel>
to every single Razor template in which he wants to use this custom helper, I would add it to the ~/Views/web.config
file, once and for all:
<pages pageBaseType="Twitter.Bootstrap.Mvc.TBootViewPage">
,然后在视图中:
@model MyViewModel
@TBoot.Foobar()
这篇关于从HtmlHelper继承而不是扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!