Browse by Tags
All Tags »
CodeDom (
RSS)
This post applies to any ASP.NET app that uses .aspx files, whether WebForms or MVC. When you write an aspx/ascx/master file (I’ll just say aspx for here on, but it applies to all), it gets compiled dynamically by the ASP.NET runtime. Note that this is true whether you use a Web Site or a Web Application Project (WAP). While in a WAP, most of the code is built by Visual Studio, the aspx pages themselves are always built dynamically . Normally, when you work with aspx files, you only need to worry about what you write in there, and the specifics of what ASP.NET generates under the cover are somewhat of an implementation details. However, in some cases it’s pretty useful to look at the generated code, either to learn exactly...
Note: this is based on ASP.NET MVC 2 RC , and will not work on earlier builds. The quick pitch: make your User Controls as cool as built-in render helpers! The goal of this post is to show you how to change the way MVC user controls are called from something like this: <%= Html.Partial("~/Views/Shared/gravatar.ascx", new { Email = "foo@bar.com", Size = 80 }) %> To something that looks just like a built-in render helper (like Html.TextBox(…)): <%= Html.Gravatar("foo@bar.com", 80) %> The current model for User Controls in MVC If you have used ASP.NET MVC, you probably know that you can use User Controls (.ascx files) to provide partial rendering. For example, the default MVC app has a Site...
Earlier this week, I wrote a post on using a BuildProvider to create ActionLink helpers . That approach was using CodeDom to generate the code, and there was quite some user interest in it (and Phil blogged it , which helped!). Then yesterday, I wrote a post on the Pros and Cons of using CodeDom vs T4 templates for source code generation . They are drastically different approaches, and while both have their strengths, T4 has definitely been getting more buzz lately. The logical follow-up to those two posts is a discussion on using T4 templates to generate MVC strongly typed helpers. The general idea here is to use the existing ASP.NET extensibility points (BuildProvider and ControlBuilder), but rely on T4 templates to produce...
There are many scenarios where the need to generate source code arises. The MVC helpers I introduced in my last post is one such example. Note that I am focusing on generating source code here, and not on scenarios where you may want to generate IL directly (which certainly do exist as well, but it’s a difference discussion). To perform the code generation, there are several different approaches that can be used. The most simplistic one is to use a plain StringBuilder and write whatever code you want to it. It’s rather primitive, but for simple scenarios, it just might be good enough. For more complex scenarios there are two widely different approaches that I will be discussing here: CodeDom and T4 templates. Let...
More Posts