1

I'm going to write a ton of helpers for my app.

For many of them I'd like not to extend HtmlHelper, but to create another helper class instead, for two reasons: 1) to be able to write, say, Link.Home and Icon.Edit instead of Html.HomeLink and Html.IconEdit; 2) to be able to easily tell standard helpers from custom ones and where the latter are defined.

Is this possible? How?

Is this not recommended? why?

thanks

2 Answers 2

4

Well, "helper methods" are just extension methods. If you want to be able to write Link.Home in your views, you'll have to extend ViewPage and add property called Link of type, say, ILink. The ILink interface may itself be empty. Now all your views should inherit from MyViewPage and writing helper methods becomes a simple task:

public static string Home(this ILink link, string text)
{
     // ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

This would be possible by subclassing HtmlHelper and calling the new class Link and Icon for example.

I would not recommend it for the simple reason that Html.HomeLink and Html.IconEdit clearly states what the method does, it outputs Html in the form of a HomeLink and and an Edit Icon.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.