Take the 2-minute tour ×

The Razor view engine in ASP.NET MVC supports @helper to define little helper methods. It seems you can do much the same by adding extension methods to HtmlHelper. In what situations is it best to use each?

share|improve this question
add comment

2 Answers

up vote 6 down vote accepted

Subjective question, so here's my subjective and biased answer: When the helper code involves amounts of C# code use a custom HtmlHelper and when it's primary markup you could use @helper. But assuming that when you have markup you could use a partial like @Html.Partial("_foo", SomeModel) or an editor/display templates like @Html.EditorFor(x => x.Foo), the @helper doesn't really have any practical use. Personally I've never used @helper by the way, and I've never recommended it's usage to people I've been consulting.

share|improve this answer
This has been my thought, especially where the example of it weblogs.asp.net/scottgu/archive/2011/05/12/… for DisplayPrice, i think this is the type of code it's best suited for. Almost completely logicless code that just affects presentation. Similarly would be to make your views DRYer. –  Chris Marisic Jun 8 '11 at 22:12
If the view engine changes we don't have to rewrite helper methods in .cs but in declarative way (.cshtml) we have to. –  Mangesh Pimpalkar Jun 21 '11 at 16:47
add comment

Yes, that's true, though the @helpers seem a bit easier to work with if there's a good chunk of markup that's included--Html extensions and more extensive markup don't go that well together, IMO.

On the other hand, @helpers can't be unit tested like Html extensions.

share|improve this answer
+1 Very good point about unit testing. –  Jake Petroules Jun 8 '11 at 22:36
add comment

Your Answer


