As a slightly nicer alternative, you can change the RenderPartial call to:

<%= Html.Partial("LogOnUserControl") %>

What’s wrong with this pattern

While this pattern certainly works, there are some issues with it. Let’s look at a few:

Hard coded paths with no intellisense

The first is that you need to refer to them by their path. In the example above, the UC is in the same folder as the master, so you can use a relative path, but in the general case, you would have to refer to it as “~/Views/Shared/LogOnUserControl.ascx”, which gets ugly. If you use T4MVC (plugging my other baby if you don’t mind!), it’ll get rid of the literal string and give you intellisense, but in essence it still works the same way: the View engine gets a literal string and tries to go from there.

Inconsistent with HTML render helpers

Another issue is that even though the UC is basically an HTML render helper, it doesn’t look like one when you call it. Ideally, you really want to call it the same way you call ‘built in’ helpers. e.g. since you can call Html.TextBox(), you should be able to call Html.LogOnUserControl(). In comparison, the RenderPartial call looks painfully complex.

Difficult to pass parameters

Suppose your UC needs to receive parameters from the caller. With Partial/RenderPartial, you have to pass those through a model object, which is painful. You can either rely on untyped data, or create a custom ViewModel type to encapsulate the data you need. In either case, it looks nothing like a call to a render helper like Html.TextBox(…), which takes ‘natural’ parameters.

How we can make this better

The tools at our disposal

There are many little known gems in ASP.NET that allow us to put together a very nice solution that solves all those issues.

The first little known fact is that Web Applications can have an App_Code folder, where all its files get compiled into a single assembly that all pages reference. Of course, the App_Code folder is very well known in Web Sites, but the fact that they can also be used in Web Application Projects (WAPs) is often overlooked.

The second little known gem is that user controls can go in App_Code! Come on, admit it, you didn’t know that. No one knows that! 🙂

The third one is that you can associate a control builder for an entire User Control (or page), and not just for controls within the page.

And finally, the fourth gem is the ControlBuilder.ProcessGeneratedCode method, which I blogged about a while back. This method lets us modify the code generated for the User Control (or page), which opens up some very powerful possibilities.

What the end result looks like

Instead of giving you all the technical details here (the full sample is attached), let’s look at what the end result looks like. In order to use it, you just need to add a reference to the MvcUserControlHtmlHelpers assembly in the sample (I know, lame name).

Then you can just move your User Control into App_Code, and make a small change to it. e.g. in the example above, you would change the directive from

The key new concept here is that you declare the parameters that you want as public fields, and you just use them in your code. Basically, think of this as declaring a method, but without a real method declaration.

And once you have that, things get really nice in your view, where you can now write

<%= Html.Gravatar("foo@bar.com", 80) %>

or if you prefer, you can write

<% Html.RenderGravatar("foo@bar.com", 80); %>

With the same distinction as between Partial() and RednerPartial(). Now for the fun, try hitting F12 (Go To Definition) on one of those methods. You’ll get:

What happened in that the special base generated some extension methods to allow you to call the User Control exactly as you would call a ‘built-in’ helper like Html.TextBox.

How does it all work

To pull this magic, it uses a combination of the various things I outlined above. I won’t go into the details since the full source is attached and has enough comments to make it clear what it’s doing. The file you want to look at is UserControlHtmlHelper.cs. I warn you, I had to use a scary trick to get around a CodeDom limitation. Also, there are some edge cases that don’t work yet but could be made to work. At this point, this is just a sample!

@Haacked: absolutely, with 4.0 generating named params with defaults makes improved things further. I actually started that way, but then chose to make something that works on 3.5. I suppose it could ‘light up’ on 4.0 and do that.

@Matt: the way I look at it, this pattern as an alternate strongly-typed way of passing data to sub-view. With a ViewModel, you create a special type instance, and pass it to the view. Here, you basically create the equivalent of a ViewModel type by declaring the public fields that you want to receive. The end result is similar, but it avoids the extra ViewModel type and makes the calling code cleaner.

That being said, I’m sure there are some scenario where this pattern is not best. I think it works well for reusable componentized UI, like the Gravatar example.

@Matt: yes that would make sense. One silly issue is that there is no easy way to get the path to the ascx while processing it in the ControlBuilder. Hopefully we can fix that in .net 4 by exposing a ControlBuilder.VirtualPath property (it actually exists, but is internal).

David, I like your efforts to utilize the hidden possibilities of the compile-time stage. I’m looking for ideas on how to strip unneeded white space characters out from the page markup during compile-time. What do you think?

David, did you check OmariO’s solution? I think possibilities of the compile-time stage are underestimated by the community and by Microsoft itself. Maybe it is not to late to add more support for this stage in ASP.NET 4? For example, Omari had to use reflection, because there isn’t ‘official’ way to do some stuff. I thinks it can be turned into the excellent unique advantage of ASP.NET. Don’t you think so?

Just used this, and it's great – much appreciated and saved me a ton of time.

One issue I found was that if the helper's public types had more than one word defining them, such as "System.string", the helper fails. I traced it to the regex. I'm no regex expert but I made this small change and it works OK for me:

In UserControlHtmlHelper.cs, around line 20, change the literal Regex:

@Dominik: are you using the technique described here, or you're asking generally about User Controls? With the techique here, I would think you should be able to just call the inner UC from the outer UC.

@Dominik: I tried and it worked fine. I would need more details on exactly what you are trying and wht you're seeing. Please post a StackOverflow question and I'll try to help there, as blog post comments dont' work well to write code.

@raulhmacias: Create a new MVC project that has the default directory structure, but contains nothing more than the user controls. Open user control's properties and set the Build Action to Embedded Resource. Then build the project and use gacutil to put it in GAC. In your two MVC apps' project, create a virtualpathprovider and register it in the global asax (HostingEnvironment.RegisterVirtualPathProvider). Then you can use the path like "~gac/MyControl.ascx"…

You will need to google a lot more about it, but this is how I did it.