Generate helper from view?

Feb 6, 2012 at 3:05 PM

One thing we have custom code to do for webforms user controls currently is to code-generate helpers from *.ascx files.  I was hoping that changing the generator from MvcView to MvcHelper would allow generation of an html helper from an MvcView but MvcHelper only works for code gen from declarative html helpers in razor.  Any chance or thoughts about creating a code generator to create helpers from the views?  I'd like to have both in our codebase so that if someone wants to do inheritance or display template APIs, they can reference the precompiled view but for just in-line rendering they can use a generated helper.

e.g. we would use convention:

MyFile.cshtml, with strongly-typed model MyFileModel would result in a helper:

@Html.MyFile(MyFileModel model);

But you could go beyond that and allow specifying specific typed parameters with a dynamic model via a special comment block (such as):

@* HelperArgs
string FirstName;
int SecondArg;
bool ThirdArg;

@Html.MyFile(string firstName, int secondArg, bool thirdArg);

We're going to write a custom generator if there are no plans to do this.

Thanks again!


Feb 6, 2012 at 4:13 PM

This sounds like Mvc T4 templates (also created by @davidebbo) with the difference that T4 templates work against controller actions rather than views. We use a combination of the RazorGenerator msbuild task and the T4 templates for the NuGet gallery and it works pretty well. Do you think you could give that a try and see if works for you? (