Skip to content. | Skip to navigation

Personal tools


You are here: Home / weblog / Bare-behind programming in ASP.NET

Bare-behind programming in ASP.NET

Posted by Dominic Cronin at Mar 18, 2007 02:35 PM |

When is a Parser Error not a Parser Error?

..... when it's a BadImageFormatException, of course.

So what's he raving on about now, you may well ask? Well nothing much except an error that cropped up today when I was fiddling around with ASP.NET. Since I got my MCAD certification a couple of days ago, I've decided to take a break from the exam track for ju-u-u-st long enough to get some sanity back on the geekery front. As I'm now fully qualified to tell the world that I'm an ASP.NET expert ;-) I figured I'd spend a little time just screwing with stuff and generally trying to break things. Following my nose, if you like.... The good old fashioned way.

So there I was trying to figure out what the complete bare-ass minimum ASP.NET application would look like, partly with a view to figuring out what sort of technique to follow when developing stuff to port over to my mono server. Interesting enough. Coded up a page:

<%@Page Language="C#" Inherits="Minimal" CodeBehind="Minimal.aspx.cs%>
<html><head><title>Minimal ASP.NET page</title></head>
<body><asp:Literal>Hello, World!</asp:Literal></body>

The code behind file just has:

public class Minimal : System.Web.UI.Page {}

Hit F5 to run in Visual Studio. KAPOW! I'm a coding genius! It works! Up pops the browser with "Hello, World!" Great!

Ummm - hang on a second. View Source.... Phew - the <asp:Literal/> tags aren't there. Job's a good-un.

OK - so stick it up on the Mono site. Should just work..... just copy over the aspx file and the cs file... Open up a browser.... Surf on over.... BOFFF!! (BOFFF is the sound that bugs make. Don't ask me why!)

Let's have a look then...

Error parsing a resource required to service this request. Review your source file and modify it to fix this error.
Cannot find type Minimal

Hmmm... maybe Mono is broken. Let's try it on a proper ASP.NET server. BOFFF!

Yup! Broken there too. What's going wrong?

It turns out that CodeBehind doesn't mean a thing to an ASP.NET server, not any kind of ASP.NET server, not Mono, and not Microsoft. CodeBehind is just a Visual Studio thing. When you hit F5 in Visual Studio, it compiles it up into a dll and drops it in your bin directory. (Hey - don't look at me like that! Whaddaya mean I should know that? I don't NEED to know that, man - I'm an emcad fer chrissakes!!! )

Ohhh-Kaaayyy - let's pop over to the Windows server image and fix that.... tum te tum...

.Net framework SDK command prompt...
cd C:\minimal
mkdir bin
csc /t:library /out:bin\minimal.dll minimal.aspx.cs

Cool - that builds OK - job's a goodun.

Hang on though - quick test.

BOFFF! Bah - getting sick of this - what now?

Error parsing a resource required to service this request. Review your source file and modify it to fix this error.
Parser Error Message: Could not load type 'minimal'.

OK - then fire up fuslogvw and check what's going wrong with the load. Ah - there's an error logged in one of the output files.

0x80131107 - WTF is that!! Quick google - aha "BadImageFormatException". Well to cut a long story short, I had compiled the dll using .NET 2.0 and the web site was configured for 1.1 ho hum.

OK - back to mono - well apart from having to add a reference to System.Web (which presumably is included by default on the Microsoft sytem)

So that worked - OK - but was it the minimal-est possible way of doing it. No - I found a couple of other ways of doing it too. It was a fun day.

If you want the functionality that I had imagined CodeBehind should take care of, use the Src attribute in your @Page directive. That way - ASP.NET will compile your bare-behind class on the fly. If you don't have any code-behind functionality, you can just code Inherits="System.Web.UI.Page", and stuff like <asp:Literal/> will just work. Actually - you can even do without the Inherits; an ASP.NET page gets compiled as a System.Web.UI.Page anyway.

But why? You may ask  - well how can I explain this. Did you ever see American Chopper on Discovery channel? It's pretty entertaining to watch a bunch of guys building a motorbike starting with a sketch on the back of a fag packet. Part of the reason it's entertaining is that they are thoroughly competent. They don't get stuck. When the gizmaflotch won't fit behind the whoffle sprocket, one of them will fire up a cutting torch or a grinding machine and remove the offending lump of metal. They can do that, because they aren't scared of it. They know that if they ruin it, they can weld another bit back on, or re-build that whole part of the bike if necessary.

So that's it I suppose - I'm practicing my welding.