Peter works on the web!

Archive for May, 2011

Using an anonymous object as the model for your Spark view

with one comment

Well another post about Spark, I’ve been spending a couple of hours with the library and I really like it. On one of my previous posts Byron Sommardahl asked if it was possible to use an anonymous object as the model of your Spark view. I proposed a solution but I didn’t really have an idea if it actually worked. Because I was curious if it would work I fired up Visual Studio.

A little warning before we dive in, my gut feeling says that the code could be prettier but it does work. Some reflection is used to get the job done.

As a base of my solution I started out with the code I’ve written in my previous post on how to generate html with Spark. I recommending rereading it before continuing. This is my new, more generic Spark view which will be used to generate HTML:

Where in the previous post we had a data transfer object with a specific type which had all the necessary data, now the Data property is of the object type. There is a second property which will hold a reference to our Anonymous type.

The Data object with which we will be working is this:

A simple Anonymous object with 4 properties will be our DataTransferObject (DTO). We will initialize our AnonymousSparkView with the following data:

Our DTO is filled out into the Data object. The type of the anonymous DTO is filled out in the AnonymousType property. Now is the time to fill out the necessary data in our template. This is the template:

This little snippet uses a bit of Reflection magic to read the properties from our anonymous object. Between the ${ } token in Spark you can put a property from your view or you can put in a c# expression. We get the AnonymousType property of our view and get a specific property of our DTO, which is an anonymous object, via the GetProperty function. Then we can get the value of a specific property by calling GetValue on the found property.

It’s not pretty but it does work. I tried out a couple of methods that didn’t use Reflection but none of them worked. I am planning of putting all future code on my GitHub account. You can find the repository here and fork it if you want. Any comments or alternative solutions are welcome, as always.

Written by Peter

May 20th, 2011 at 9:34 pm

Posted in Spark

Spark HTML generation fails: View source file not found

without comments

I use Spark as engine to generate all sorts of HTML but on Monday last week that specific code snippet failed without being changed. Spark indicated that it could not find the view file, so I checked and double checked that the view was in the correct place. This was as it should be, but still Spark threw the exception that it could not find the view source file. I decided to try the same code on my laptop and there Spark still worked as it should, so the issue was related to my development machine configuration.

When I further investigated the odd exception that was thrown, I saw that the inner exception was an AspNetHostingPermission exception.

So I checked all the rights on the folders where the view files resided and found nothing out of the ordinary. I really couldn’t find anything that could’ve broken the HTML generating. So it was back to the drawing board, in my case Google, to search for possible causes and solutions. After a lot of searching and not finding very much answers I stumbled onto the following post. The error in that post was the same error that I got, but I was obviously not working with Windows 7 RC.

As solution the author states that you have to enable the ‘Load User Profile’ property of the application pool under which your web application runs. You can find the property in the advanced settings dialog of the application pool. Because being a bit really desperate I tried out the given solution and although I was rather sceptic to my surprise it worked.

I have no idea how that property got switched off in IIS, but if you ever have the same problem you’ll hopefully won’t look for a solution as long as I did. You’ve got the solution right here! ‘Till next time.

Written by Peter

May 8th, 2011 at 7:03 pm

Posted in Exception,Spark