Razor: Declarative HTML helpers
The ~/Views/Helpers
location as describe in that ScottGu post will not work in MVC 3 RTM. At the time that article was written it was a design idea we were tossing around but ultimately we were not able to implement it.
Putting your helpers in App_Code works but has certain limitations that impact certain MVC scenarios (for example: no access to standard MVC Html.
helpers)
Simple workaround:
In each helper add argument WebViewPage page.
App_Code\Test.cshtml:
@using System.Web.Mvc.Html
@using System.Web.Mvc
@helper HelloWorld(WebViewPage page)
{
@page.Html.Label("HelloWorld")
}
This helper usage in any page:
@Test.HelloWorld(this)
As marcind said, we weren't able to support the ~/Views/Helpers
location due to some limitations in our compilation model.
We're working on a better solution for declarative HTML helpers, but it won't make v1. For v1, we have two solutions:
- App_Code (which has some problems as Marcin mentioned, but does work)
- David Ebbo (member of the team) has a Visual Studio add-in that compiles them into your DLL