New adventures under medium trust

Many web hosting companies only allow ASP.NET applications to run under medium trust. This has been a major drawback for Cuyahoga because it required full trust (or better: some libraries require full trust). This has already caused some nasty surprises when people deployed their site to the host to find out it would not run.

Well, I can finally say that Cuyahoga 2.0 will work under medium trust!

Castle DynamicProxy

Last week, someone mentioned on the Castle users list that DynamicProxy was supposed to work under medium trust and this immediately triggered me. DynamicProxy had always been the key part that prevented Cuyahoga, NHibernate, and lots of other software from running under medium trust because it generates assemblies on the fly and that’s not allowed (at least pre-NET 2.0 SP1).

So, I checked out a fresh version of the Castle trunk, built it with AllowPartiallyTrustedCallers and copied the assemblies to Cuyahoga that was set to run under medium trust.
Unfortunately, still no luck. The dreaded SecurityException showed its yellow screen, but instead of giving up immediately, I took a deep breath and started digging the DynamicProxy sources. The solution was a simple one: DynamicProxy calls AssemblyBuilder.DefineDynamicModule and used the overload that generates debug symbols. Changing that to not generate the debug symbols anymore made it work under medium trust! I send a patch to the Castle guys and lets hope it can be incorporated. This allows NHibernate to run under medium trust without turning off lazy-load on class mappings or using a special proxy generator.

One caveat: the Castle trunk requires .NET 3.5, so we can’t fix it for the 1.6.x branch of Cuyahoga which is .NET 2.0.

Lucene.Net

Second, Lucene.Net didn’t work under medium trust and boy, that was easy to fix: a single call was made to a relative file path, which is not allowed. Changed that to use the full path and it worked. Submitted a patch and I hope they will accept it.

Cuyahoga

So, with the libraries working under medium trust, I was ready to roll, at least I thought so. It appeared however that Cuyahoga also did some nasty things that are not allowed under medium trust, such as requesting a HttpModule instance from the appdomain and some file access outside the application root. Fortunately these were easy fixes and now I have everything working just fine under medium trust.

I think, I’ll leave medium trust turned on in the development version to signal issues in an early state.

The problem of data grids in your web application

I’m sure everybody who builds web applications uses grids to display data. There is nothing wrong with that per se, but you might not realize that you’re increasing the customers expectations to an unreachable level:

Yes, all very nice and well, but can’t you make that thing work like Excel?

My first thought is always something like: “sure, if you supply us with the budget that the Excel team has”, but in practice we’ll always end up buying one of those bloated grid components or hacking our own solution where the customers is never 100% happy (because it still doesn’t work like Excel).

The solution?

Don’t format the data in a grid, but in a nicely formatted list and stay away from anything that resembles column headers. Add some simple search and filter options and people are happy.

ASP.NET WebForms and MVC together in one project

This post briefly describes a solution to mix ASP.NET WebForms and MVC in one project. You can download a sample project that might be more useful than my ramblings. Download the sample here, unzip, open with VS 2008 SP1 and hit F5.

There are lots of ‘legacy’ ASP.NET WebForms applications out there in the wild. What if you want to create new functionality or rebuild an existing part with ASP.NET MVC, but you can’t (or won’t) create a separate new project?

The simple solution is to add the default MVC folders (Content, Controlllers, Scripts, Views etc.) to the root of the web project, add references to the MVC binaries, modify web.config and add the MVC GUID to the ProjectTypeGuids in the project file. A major drawback however is that it pollutes the root of your carefully structured application. This post will show you how you can logically separate the new MVC pieces from the existing WebForms app in a single project.

Isolate the MVC bits

webforms-mvc-solution  A simple requirement: I want that all my MVC folders reside in the folder /MyMvcApp and I also want that all urls of the MVC app start with /mymvcapp. No interference with my old WebForms app please.

Area’s

About a year ago, Phil Haacked showed a concept called area’s to partition an ASP.NET MVC application. After that, Steve Sanderson came up with an improved version of the concept.

Although designed to partition an MVC application, area’s are also useful when we want to isolate our MVC application from our existing WebForms app. The solution from Steve Sanderson almost completely fits our needs, follow the link for more technical details. The only thing I removed was the usage of an Areas subfolder in the project where all the individual MVC sub-applications. Simply didn’t need the extra subfolder and url of the MVC app is closer to the physical structure.

So, I implemented area’s with a specific AreaViewEngine that can lookup views based on the area name and added the extension to the RouteCollection class to add area information when mapping routes. This is how Global.asax.cs looks:

protected void Application_Start(object sender, EventArgs e)

{

    ViewEngines.Engines.Clear();

    ViewEngines.Engines.Add(new AreaViewEngine());

 

    // Routes

    RegisterRoutes(RouteTable.Routes);

}

 

public static void RegisterRoutes(RouteCollection routes)

{

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });

 

    routes.CreateArea("mymvcapp", "WebFormsMVCDemo.MyMvcApp.Controllers",

        routes.MapRoute("mymvcapp-defaultroute", "mymvcapp/{controller}/{action}/{id}", new { action = "Index", controller = "Home", id = "" })

    );

}

With the ViewEngine and routes in place, we’re able to run the MVC app from the url /mymvcapp. To make sure the WebForms url’s still work, we just have to add some ignore statements in Global.asax.cs (in our case, exclude .aspx and .ashx from routing):

public static void RegisterRoutes(RouteCollection routes)

{

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.IgnoreRoute("{*allaspx}", new { allaspx = @".*\.aspx(/.*)?" });

    routes.IgnoreRoute("{*allashx}", new { allashx = @".*\.ashx(/.*)?" });

    routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });

 

    routes.CreateArea("mymvcapp", "WebFormsMVCDemo.MyMvcApp.Controllers",

        routes.MapRoute("mymvcapp-defaultroute", "mymvcapp/{controller}/{action}/{id}", new { action = "Index", controller = "Home", id = "" })

    );

}

One caveat: when performing the request for /mymvcapp without controller name (and action), the Default.aspx that comes with the MVC projects was loaded, but area’s seem so cause a problem with the default one. Instead of rewriting the url from ‘default.aspx’ to ‘/’, I had to rewrite to url to ‘Home/Index’:

public partial class _Default : Page

{

    public void Page_Load(object sender, System.EventArgs e)

    {

        string pathToRewriteTo = Request.Path.ToLowerInvariant().Replace("default.aspx", "Home/Index");

        HttpContext.Current.RewritePath(pathToRewriteTo, false);

        IHttpHandler httpHandler = new MvcHttpHandler();

        httpHandler.ProcessRequest(HttpContext.Current);

    }

}

I’m under the impression that I’m missing something very simple, but couldn’t get my fingers behind it, so if you know it, please comment.

 

That’s it. I created a sample project with both WebForms and an MVC app in a subfolder. You can download it here.

ASP.NET MVC bridging the gap with PHP development?

Grand_Canyon6 For a long time, there has been a distinct separation between ASP.NET and PHP developers. The platforms have been so fundamentally different and also have the surrounding cultures. Generally speaking, ASP.NET developers consider the PHP guys script kiddies with no real understanding of ‘real’ software development and the PHP guys saw ASP.NET developers as some M$ infected bunch of people that have no clue of web standards, clean HTML or how to build a proper web app at all.

But times have changed. Both platforms are moving. PHP now has a whole bunch of application frameworks (Zend, CakePHP etc.) that encourage good application design and also since PHP 5, the programming language has improved a lot. On the other hand, with the release of ASP.NET MVC, it has become much more easy to render clean HTML and we can finally leverage all the cool client-side toolkits that are out there without having to work around the quirks of Web Forms

So, with this movement, we see that both platforms grow towards each other. More and more developers come to have a look over the fence to see what’s happening at the neighbours place.bridge-gap

Two concrete examples:

  • I’m consulting a little at a PHP shop that is moving towards .NET. They found Visual Studio and C# very cool, but the Web Forms issues regarding clean HTML etc. made it an absolute no-go. ASP.NET MVC is what made them switch.
  • Just watched the ASP.NET MVC Mix 09 presentation of Rob Conery (Microsoft) and he showed lots of things that were clearly inspired by how PHP apps like WordPress work.
    I like this movement! As a .NET developer, I find it very refreshing and inspiring to mix and mingle with ‘the other side’. There is a lot we can learn from each other to make better web applications.

Useful Cuyahoga Modules – The Flash module

Last weeks, I’ve been working on a project that uses Cuyahoga as the CMS. For this occasion, I’ve (temporarily) turned from Cuyahoga framework developer to a Cuyahoga consumer. In this role I’ve learned to appreciate some contributed modules that I didn’t really know that well, but appeared to be very valuable.

Today, I’d like to pay some attention to the Flash module that comes with Cuyahoga 1.6.0. It allows to embed Flash content in your site, but it also gives you some nice extra’s:

  • Optionally add a text representation of the Flash content for search engines or users that don’t have Flash installed;
  • It uses swfobject.js for automatic plugin detection and prohibiting the nasty ‘Click to activate’ message in IE;
  • It allows adding Flash parameters and FlashVars per section.

With the last option, you can for example embed a Flash movie player (the .swf file) and then point it to a .flv movie via FlashVars. Even with the absence of a cool media module in Cuyahoga, you can still dynamically add media content to your site this way.

Very nice!

ASP.NET development is like my pedalboard

Warning: this post doesn’t only contain the usual software dev stuff, but also some serious guitar-geekiness.

This morning, I was shocked when I found out how much the development of my pedalboard went through exactly the same stages as the way I’m building ASP.NET web applications. First let’s see where I’m coming from:

pedalboard-before

In the picture above you can see a large yellow device. This is the Line 6 Distortion Modeler. It claims that it can produce all the classic distorted guitar tones that you’ll ever want and to a certain extend this is true. The sound is alright and the possibilities are huge. But also: the output (sound wise) that this device produces is always less than the original pedals that it’s trying to emulate. Also, the switches are a little bit unreliable and cause a little gap when changing sounds. I tried to fix this, but the device is very inaccessible and doesn’t lend itself very well for modifications or extensions.

Less is better

These days I’m back to where I came from: two simple pedals that don’t have a gazillion options but just do their work and are very efficient:

pedalboard-after

On the left we have the Proco RAT and on the right it’s the Digitech Bad Monkey (‘told you we’d have some guitar-geek talk in this post, remember?). These guys are simple, reliable and their output is just the way I want my distortion to sound without losing dynamics and tone. Full control!

So, what does this have to do with ASP.NET web development?

Nothing of course :) It just struck me that at about the same time I switched from ASP.NET Webforms to MVC, I also changed my pedalboard to accomplish the same result: making the end result better with a more simplistic approach. The yellow monster in the first picture is like Webforms with ASP.NET AJAX where I have a love-hate relationship with. The two pedals in de second picture feel like the MVC and jQuery combination (loosely coupled, only connected with a little patch cable), that brought a lot of fun lately and work very well for me.

Yeah, great analogy, NOT! I didn’t subscribe to read this blah about some guitar thingy.

Well it just something that went through my mind this morning and writing about it also gives me a chance to post some crappy gear pictures, like it or not. Here’s another one for free:

family

;-)

P.S. the empty spot above the pedals is normally reserved for my Electro Harmonix Memory Man Deluxe but this drama queen is broken at the moment and deserves a single post of its own.

How can we make the SharePoint world a better world?

Lots of companies are positioning SharePoint as their preferred platform for everything that has to do with the web, both intranet and public facing websites. Now out-of-the-box, SharePoint delivers excellent value, but when building custom functionality or public facing sites, there are some serious issues.

My biggest gripes are:

  • The whole development experience is awful. Yes, you can create custom webparts, but I’ve yet to come across the one that isn’t a big ball of mud. Also, the entire cycle of compiling, deploying and testing takes way too long to be productive at all. One minute is pretty common. Gosh, I wonder why all these projects are late every time ;) ;
  • The HTML output is a mess. Not so much a problem for intranets, but for public sites it is. With a zillion tweaks, it is possible to fix it, but then again, no wonder these projects are late every time;

I’ve been thinking how to improve this situation. Sure, I could say: “just don’t use SharePoint for your public web sites”, but companies are going to push everything to Sharepoint anyway. The whitepapers and consultants will tell them that everything is possible and it’s from Microsoft, so it’s a safe bet. We (as in Microsoft developers) simply have to deal with it the next years.

A Cunning Plan

First: let’s focus on publishing sites. I want to have:

  • Clean standards-based HTML;
  • Clean url’s;
  • The ability to add custom applications as a feature without being restricted to webparts, or even better: develop applications standalone and deploy to Sharepoint when they’re ready;
  • Leverage the SharePoint infrastructure (sites, lists, security, etc);

Looks impossible but is it really?

Couldn’t we not drop in System.Web.Routing, create a special SharePointRouteHandler, that sets up required sharepoint infrastructure and delegates the request to a Controller to have ASP.NET MVC within the SharePoint context? I think it’s hard and complex, but not impossible. We could leverage all strong points of ASP.NET MVC like separation of concerns, testability, clean HTML together with the huge foundation that SharePoint offers.

Plan B

Microsoft is already working on this situation and the next version of SharePoint will address the situation properly.

Now, please tell me that I’m a: ambitious, b: naive or c: completely insane :) .

Paging demo upgraded to ASP.NET MVC RC

The sample project that belongs to the Paging with ASP.NET MVC post is upgraded to ASP.NET MVC RC. Get it from here.

Hey, something’s changed!

Today, I moved this blog from SubText to WordPress. All was going fine with SubText, but WordPress is so much more sophisticated these days, I couldn’t resist it :) .

Since the server is running IIS6 and not the usual LAMP stack, I was prepared for some struggling, but it was pretty easy.

I started with installing the IIS FastCGI extension, PHP 5.2.8 and MySQL 5.1.30. After that, I only had to install WordPress and things were ready to roll.

For pretty extensionless urls, I found the Ionics Isapi Rewrite Filter that also redirects the old SubText url’s to the new pretty ones.

The most challenging part was migrating the old posts from SubText to WordPress, but luckily, I wasn’t the first one who attempted this.

Please leave a comment if something is broken, or when you find links that point to the old blog.

Paging demo upgraded to ASP.NET MVC Beta

The post about paging in ASP.NET MVC still gets many hits, but the demo was created with ASP.NET MVC preview 5, so therefore I upgraded the demo to ASP.NET MVC Beta. I also removed a little bug that occurred when paging an already filtered list.

You can download the updated demo here.