Inheriting from HtmlHelper instead of Extending it

杀马特。学长 韩版系。学妹 提交于 2019-12-22 12:18:07

问题


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:

  • Extend the HtmlHelper prefixing the methods with TB
  • Create a new class TBootHelper which contains the methods

In the second case, to get the TBoot helper available, the developer would add

<pages pageBaseType="Twitter.Bootstrap.Mvc.TBootViewPage">

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);

My question is, Should I create a TBootHelper class or just add methods to the HtmlHelper?


回答1:


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.

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

and then in the views:

@model MyViewModel
@TBoot.Foobar()


来源:https://stackoverflow.com/questions/9456953/inheriting-from-htmlhelper-instead-of-extending-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!