Archive for category ASP.Net

Learning Windows Azure

I started to learn how to use the Azure Cloud Service from Microsoft this week. Currently it’s still in Tech Preview stage. Unfortunately you can tell that from the SDK documentation.

Here’s some useful links to get you going:

  1. Screencasts:
    These are quite basic, but trust me, you need them to be basic to get you started.
  2. SDK:
    Contains CHTM-style documentation, tools and samples. Don’t expect too much from the docs; they explain enough to get you confused, and then have an API reference. You need to unzip the samples and get into them to start understanding how everything fits together.
  3. Visual Studio Templates:
    This gives you a set of project and item templates which you can use to create and publish Azure applications. Don’t worry about the extra projects it adds to a solution, or the config files. You will learn more about them later.
  4. The Azure developer center on MSDN:

Assuming you already registered for Azure, that’s all you really need to get started.

The biggest problem I found at first was deploying an app. Once you have generated an Azure project in Visual Studio, you expect to be able to publish it from Visual Studio too. Unfortunately you can’t, and it takes a little more effort. I’ll write more about that in another post.

I also needed help trying to understand what to focus on to get started. So here’s a big tip: Ignore .Net Services, Live Services, and SQL Data Services. They aren’t part of Azure per se. You can come back to them later. First you just need a hosted project and some storage – either blob storage or table storage. (There’s also queue storage, but I bet no one will want to use that straight away – it’s for tying two apps together, which no one will want to do at first.)

I recommend you download the SDK and the Visual Studio templates, create yourself a “Web Role” project (which is really the equivalent of an ASP.Net project), and work on that. Then move onto table and blob storage. You can use the screencasts to help you.

Good luck getting started!

No Comments

C# Acrobatics : Lambdas and Expression Methods as a replacement for NVelocity

I’ve been very quiet recently. (I’m trying to not be so loud, Scott. 🙂 ) You see, I’ve been writing a lot of ASP.Net code for a site I’m working on. And, to be honest, I’ve been having a lot of trouble. The source code for .Net has been very helpful, and I’ve learnt a lot about what’s going on under the covers of ASP.Net because of it.

(Note: I won’t comment here on the quality of the code I’ve found – I’ll leave that up to you to judge. But in any case, I’ve been trying to build on top of it.)

One thing I’ve found to be important is the reliance on Web Controls. (It’s got something to do with javascript libraries, but that’s another story.) Getting away from the "standard" way to do ASP.Net isn’t easy though. Even the ninjas on the ASP.Net MVC team seem to be having trouble. However, with the magic of lambdas and extension methods in C#, I think I might have just about managed to get something usable. I thought I’d publish my work here, and see what comments I got.

I think it’s best to start with what my ASP.Net code looks like once I’ve got everything working. (Notice I still have some Web Controls in there, but that’s because I’ve not worked out how to do sorting of data without web controls yet.)

The inspiration for this was taken from the improvements made to NVelocity by the gurus on the Castle Project. I thought it looked great, and I’d like something similar, but I didn’t really want to learn a whole new scripting language and integrate it into my working environment just for rendering a bit of HTML. So I built some C# classes to do a similar thing for me instead. It’s not as nice as NVelocity, but it’s okay for now.

Warning: The following code may contain statements of a disturbing nature to more sensitive readers. We cannot be held responsible for any confusion, delusion or mental illness caused by this code.

It starts by taking a collection of Task objects, and calling the extension method "ForEach" on them:

<% Tasks.ForEach(sections => {
   sections.NoData = tasks => {
   Hey, you've got nothing to do.</p>
   sections.BeforeAll = tasks => {
  <table class="task-list">
    <tr class="task-list-header">
        <asp:LinkButton runat="server" CommandName="Sort" CommandArgument="StartDate"
          Text="Started" />
        <asp:LinkButton runat="server" CommandName="Sort" CommandArgument="DueOn"
          Text="Due" />
        <asp:LinkButton runat="server" CommandName="Sort" CommandArgument="Priority"
          Text="Priority" />
        <asp:LinkButton CssClass="task-description" CommandName="Sort" CommandArgument="Title"
          Text="Description" runat="server" />
<th> </th> </tr> <% }; sections.Before = task => { %> <tr class="<%= this.tableCssClasses.Next() %>"> <% }; sections.Each = task => { %> <td> <div class='calendar calendar-icon-<%= task.StartMonth %>'> <div class="calendar-day"> <%= task.StartDayOfMonth %></div> </div> </td> <td> <div class='calendar calendar-icon-<%= task.DueMonth %>'> <div class="calendar-day"> <%= task.DueDayOfMonth %></div> </div> </td> <td> <%= task.Priority %> </td> <td class="task-title"> <a href='<%= Href.For("~/Tasks/{0}/Show.aspx", task.ID) %>'><%= task.Title %></a> </td> <td> <asp:Button ID="Button1" runat="server" CssClass="button" CommandName="Delete" Text="Mark Done" /> </td> <% }; sections.After = task => { %> </tr> <% }; sections.AfterAll = task => { %> </table> <% }; }); %>

It might take a while to grasp what’s going on here. The code actually starts using an Extension method to IEnumerable that looks like this:

public static void ForEach<T>(this IEnumerable<T> enumerable, ForeachSectionSetter<T> sectionSetter) {
  if (enumerable != null) {

    if (sectionSetter != null) {
      ForeachSections<T> sections = new ForeachSections<T>();

      if (enumerable.Count() == 0) {
        if (sections.NoData != null)

      if (sections.BeforeAll != null)

      int itemIndex = 0;
      T previousItem = default(T);

      foreach (T item in enumerable) {
        if (sections.Before != null)
        if (itemIndex % 2 == 1 && sections.Odd != null)
        if (itemIndex % 2 == 0 && sections.Even != null)
        if (itemIndex > 0 && sections.Between != null)
          sections.Between(previousItem, item);
        if (sections.Each != null)
        if (sections.After != null)
        previousItem = item;
      if (sections.AfterAll != null)

The delegate ForEachSectionSetter is used by the calling method with a lambda expression. As a parameter it receives an ForeachSections object, which looks like this:

public class ForeachSections<T> {
  public Action<T> Each { get; set; }
  public Action<IEnumerable<T>> BeforeAll { get; set; }
  public Action<T> Before { get; set; }
  public Action<T,T> Between { get; set; }
  public Action<T> Odd { get; set; }
  public Action<T> Even { get; set; }
  public Action<T> After { get; set; }
  public Action<IEnumerable<T>> AfterAll { get; set; }
  public Action<IEnumerable<T>> NoData { get; set; }

The calling method gets the chance to set the properties of this class before it is returned to the constructor of the ForEach method for processing. And because each property is already preset to a default value (Null in this case), the constructor can use the ForeachSections object just like a set of default or optional parameters. The caller can simply set values to the properties it needs, and ignore the rest.

If I had tried this another way, using overloadable constructors, it would have led to multiple constructors with indistinguishable signatures. If I’d have used property initializers, I wouldn’t have been able to run the whole routine without requiring a second call to the object, which actually wasn’t possible.

Basically, I couldn’t think of another way to do it.

The properties of the ForeachSections object are all delegates too. That means that we can use them with lambdas, which gives us lambdas inside of a lambda. (Hmm, very confusing!)

So what do you think? Could you use something like this? Can you make it simpler? Leave me a comment if you can.

, , , , ,

No Comments

The Value of Being Free to See the Source

Since the source code to ASP.Net was made available, I’ve been using it extensively. Here’s a great example of why it’s so valuable.

I’ve been trying to integrate the Enterprise Library 3.1 Exception Handling Block into my application. My application is split into a core and web UI specific components, so I’ve defined errors in my code to be thrown when a resource is not available. The web application configuration file specifies that if a specific exception, e.g. a ResourceNotFoundException, is thrown, the Exception Handling Block should replace that exception with a 404 Resource Not Found error using Http. That should in turn use the CustomErrors feature to redirect to a 404 not found page.

Makes sense, and sounds simple, don’t you think?

Nothing in the docs says that it shouldn’t work.

But it doesn’t. It simply won’t work. Why? Well, there’s nothing on the web. But after spending some serious hours digging through the source code, I can finally see why.

Here’s a lovely little hidden-to-the-world snippet of the code I got inside of Visual Studio:

code = HttpException.GetHttpCodeForException(e);

// Don't raise event for 404.  See VSWhidbey 124147.
if (code != 404) {
  WebBaseEvent.RaiseRuntimeError(e, this);

So it would never work!

Nice of them to let me know.


1 Comment

Mix 08 Online Presentations

My favorite conference is about to start: MIX 08.

It’s a great conference for Microsoft developers with an interest in the web. Last year they had some great talks about user experience and architecture. There’s always something interesting to learn.

Unfortunately I can’t be there (as usual), but all the sessions will be online. Tim Sneath just posted details of where and when you can watch them:

  • The keynote with Ray Ozzie, Scott Guthrie will be available live at 9:30am Pacific / 5:30pm GMT on three streams: 750kbps, 300kbps, 100kbps.
  • The breakout sessions and panels will be online within 24 hours of each session at MIX08 sessions.

No Comments

A Pure ASP.Net Grid with Grouping

One of my favorite bloggers is Matt Berseth. Nearly once a week he comes up with a post where he does something amazing with the standard ASP.Net controls. I usually read his posts in awe. He’s really good.

But he’s not only is a good developer, he’s a great writer. Even though his posts are concise and straight to the point, most of them are pages long. You can really learn a lot from following his instructions.

He’s so good, in fact, that he regularly gets a mention on Scott Guthrie’s posts.

One of my favorite posts of his was where he used LINQ-to-SQL, a LinqDataSource control, and an ASP.Net ListView control – all new in .Net 3.5 – and made a grid with grouping functionality. He did it all in a standard way, and didn’t use any funny tricks.


Here’s a link to the article:

Building a Grouping Grid with the ASP.NET 3.5 LinqDataSource and ListView Controls


, , , ,

No Comments

Start Learning Silverlight 2.0 Now

Now this is exciting. (Well, if you’re a geek.)

Silverlight 2.0 is on its way, and Scott Guthrie has posted 8 tutorials about using it.

I’m off to read them now…

, , ,

No Comments

The LinqDataSource and the Hidden Viewstate

Yesterday I thought I’d learn about the LinqDataSource in ASP.Net 3.5, and got an interesting surprise.

The new LinqDataSource can also be used with a LINQ-to-SQL model to perform updates. You simply add the DataSource to your page, set the table name, and set EnableUpdate to true. Then, using a standard DataControl, you can make updates to your data entities.

The question is, how does this work? It appears to be a bit magical. Read the rest of this entry »

, , ,


My own ASP.Net MVP Framework

So, I did it! I finally did it!

I wanted to publish my ideas for a framework, and I did. You can find it on CodePlex at

Go have a look. I really want to know what you think.

Read the rest of this entry »

, ,


ASP.Net MVC Corollary – What to do?

Dude! I got quoted! And by none other than Rob Conery of SubSonic fame.

It seems like my last post caused quite an unexpected stir. Thanks to both Rob and Scott for taking the time to answer me. I really appreciate it.
Read the rest of this entry »

, , ,

No Comments

.Net Source Code Now Available

Scott Guthrie has just announced that the source code for the .Net framework has just been made available for reference use.

It will be particularly useful to see how the controls in ASP.Net and Windows Forms have been done.

Detailed instructions for how to set it up are here.

One caveat: it’s not available for the Express editions of Visual Studio. Shame! I was just starting to have some fun with them too.

, , , ,

1 Comment