Don't hate me for my .NET

I’m an outcast trying to fit in…

I’m an outcast trying to fit in… at an event like FOWA I’ll feel like working with .NET is akin to admitting to owning the Rednex single ‘cotton eye joe’ from the glory days of ’94… but then I’ll go to a Microsoft event and feel like a pariah obsessed with miniscule details that I shouldn’t worry about (unobtrusive javascript.. element ID and exact markup control…).

A generalisation this may be but it highlights the difference in perception between languages such as PHP/Ruby and .NET in the world of web. So much so that people often don’t even list .NET when they reel off examples after the phrase ‘your language of choice’.

I don’t so much want to ‘put the record straight’ just to put forward my experience starting out with PHP and moving to .NET, with a sprinkling of the stuff I usually hear against .NET.

Background

Following a pretty standard path I started out in web development with static HTML, moved on to being scared by CGI then dabbling with javascript and later some PHP and Actionscript (with some VB6 and Java studying thrown in there for good measure too). This led to my first full time role being as a PHP developer, which later evolved into a C# position.

.NET WebForms vs. Everyone Else

The first thing I (and many others) realise when moving to .NET (Webforms) is that it’s really not the same paradigm. In fact I’d go so far as to say that if you skipped straight to learning Webforms, you haven’t learnt web development. What you’ve learnt is winforms. And the reason for this is that that was the intention of Webforms – to assist winforms developers in making the leap to the web and to mask the lack of state.

The ‘innovation’ therefore of postback/viewstate persisted server controls was that as a developer you didn’t have to worry about working in a stateless environment as you did with other languages. Nor did you really have to worry about the actual markup output (in the same way that you wouldn’t worry about how the pixels rendered for a button in a windows app). Submit buttons and Select boxes can have server side Click and IndexChanged events and before you know it you’re well away without worrying about html or javascript or even css (if you went with some rather questionable attributes available on server controls for presentation).

What this caused however was a largely negative view of .NET from the non .NET side of the tracks, and it’s generally the sort of stuff I continue to hear.

However this is not an attack on .NET (whether people know it or not). This is an attack on a particular way of coding in .NET webforms. Yes, Microsoft pushed .NET to be used in those kind of ways but that doesn’t mean you had to do it that way.

In the same way that developers don’t use the visual interface of Dreamweaver to generate their markup .net developers don’t need to use server controls and viewstate. Just because it’s there and assists some people in their needs doesn’t mean everyone should use it.

Much of my recent work for example has relied heavily on generating Xml as the content and using Xsl to create the presentation, thus bypassing the majority of asp.net server controls and their associated scripts/viewstate/long ids.

Without taking this approach you can still make your own user and custom controls, but be smart about how you make them. Turn off viewstate when not needed, use standard html controls when you don’t need the functionality provided by an asp server control and use repeaters with your own standard html templates rather than .NET rendered gridviews.

.NET or not .NET?

It could be argued that doing these kinds of things is not ‘.net’ but thats not true. It may not be entirely ‘.net webforms’ but you are still using the power of the .net frameworks class library. You are still using the, in my view currently unchallenged awesomeness of, Visual Studio (and yes I’ve used eclipse.. it’s not as amazing as you might like to think) and you’re still getting the ability to write in the same language for your website, windows services, silverlight, xbox and windows applications.

Admittedly it’s unfortunate how long it has taken for the release of asp.net MVC (although lets be honest PHP weren’t flying off the blocks with full and complete OO support so no-ones perfect). However the key is, as with so many things, that the quality of your .net output is down to the quality of the development.

Wrap up

I too, when I was asked to swap to .net from PHP, thought .net was an overcomplication. “I want to just call the database and do a loop outputting html in my markup. I don’t want to put a GridView in the markup then in a ‘code behind’ ‘load event’ access the database and databind to the grid”.

It’s a leaning curve as with all things. My early PHP was, going back on it, messy to read and understand as it was all mixed in with markup with include files all over the place. Similarly my early .net work probably relied on an overuse of server controls. As you get in to something you get better at it. And although .NET seemed like a greater move away from static pages than PHP (which is probably why I saw it as an overcomplication) I now personally love working with it.

I’ll wrap this article up briefly with some reasons I like .net that either I haven’t found at all, or have found harder to do in other environments/languages.

  • Similar syntax to java/javascript/actionscript
  • Reusable classes across web applications, windows applications, windows services, IIS modules, xbox games
  • Very easy to cache with a large variety of inbuilt assistance
  • Extremely OO friendly

I’m not saying .NET is the best, or amazing, but its certainly not something to be looked down upon or avoided purely because it’s Microsoft or because of a misunderstanding surrouding it. Over the time I’ve been using it it’s got better and although the picture I paint in the intro to this article is still true, it is less so nowadays. Css Control Adapters have been around a long time to assist with poor rendering, MVC is upcoming, and along with things like ClientID mode the bad things are going or easily avoidable.

I’m not sure I’ve covered everything I would have wanted to in this post, as it is such a vast area and this was really just a one sided conversation from the POV of .NET not being quite as sucky as many people think. Please fire any points gripes or questions at me.

Mini Glossary (for those .net virgins among us)

One form to rule them all
Webforms asserts that each page has one form that contains all elements rather than multiple forms as and when necessary. Or to quote…

For example, most ASP.NET Web pages contain a single server-side Web Form, with multiple Web controls inside the Web Form. The Web Form is an HTML control (System.Web.UI.HtmlControls.HtmlForm). Those Web controls inside the Web Form are children of the Web Form.

Postback
Form field data is posted to the server and used to repopulate form field values automatically e.g. if you change an instance of a textbox server control before posting back .net maintains the value you posted from the first screen. Similar to the following manually achieved effect in PHP:
<textarea name="body" id="body" cols="40" rows="5">
     <?php echo htmlspecialchars(stripslashes($_POST['body'])); ?>
</textarea>
Viewstate
The encoded (and optionally encrypted) first hidden field used by .NET as a means to persist programatically set control properties.
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTMwMTcxNDUyNmRk4HWhWoozWLYUelHWkPh3E67bXGo=" />

A control will gain its final value after the following cascade: Initial declarative value, Posted value from Request.Form (if postback), ViewState value (if changed via code after a postback).

Crazy IDs
Until the upcoming 4th version of .net developers were not trusted to know if there server control IDs were unique and because the built in viewstates, postbacks etc relied on it someone decided to make damn sure that control IDs were unique and context based on their location in the control heirachy. This led to ids like “ctl00_asp_menu_links_ctl03_contextLink”. Which, if someone added a parent or reshifted markup slightly, would change. Particularly helpful for clean client side development.

This post was written by Craig Rowe, developer at Headscape. For more posts from Craig visit his blog.

  • http://www.mmwebdesign.co.uk Chris Morledge

    Totally agree with this article, .net winforms has alot to answer for when it comes to the reputation of .Net developers amoungst standard complaint designer/developers. When I first tried the winforms model after being a ASP developer for many years part of me loved it’s rapid development but I always felt a bit dirty when looking at the source code on the resulting page. Winforms will probably be the first impression of .Net for most newbies so I can see why some instantly turn their nose up at it.

    Learning to step out of the winforms mindset and take control of my code, made me realise the power of .Net while still being able to take advantage of all the above listed benefits.

    With the recent enhancement including JQuery integration and MVC (which has been released) and knowing my skill set is easily transferrable to many other programming environments, I can only say thumbs up to .Net and anyone brave enough to admit they are .Net developers :)

  • http://www.frederikvig.com Frederik Vig

    Nice post. The main thing ihmo, is regardless what your programming language of choice is, you can still produce bad code (PHP/ASP classic spaghetti code comes to mind..). Its up to us, the programmer, to write good code. Fortunately the .NET framework, Visual Studio etc makes this so much easier :).

  • Tim Snadden

    Interesting article. I still think that to a certain extent you are swimming against the tide using .Net this way. All of the training material and tutorials will show you how to do it the easy way.

    The other thing is the smell of Microsoft. Internet Explorer is such an abomination and is single-handedly holding back/overcomplicating client side development and this can spill over to a negative perception of all Microsoft products.

  • Mike

    Great post.

    Generally it’s just another easy pop for web designers/developers who haven’t even tried it, and can jump on the Anti-microsoft bandwagon.

    Too easy to slag it off without even trying it.

  • http://fellowshiptech.com cmv

    We made the decision to abandon webforms nearly 2 years ago and haven’t looked back (we crafter our own pseudo MVC framework). In terms of a compiled language, IMO, C# is the best and consistently getting better. There are two real barriers to entry I see with .net (and .net MVC) moving forward. The tools are way too expensive (the free ones are sub-par) and IIS is a terrible web server. If they can somehow provide good free tools (or reasonably priced tools…including a decent html editor) and provide the ability to script against IIS without having to write low-level C code, then they can begin to make traction outside of the corporate world.

  • http://alternativeto.net Ola

    Nice to read a post like this. It’s so true that you feel a bit like an outcast if you use .net in the “web 2.0 startup” world.

    And .net is a great platform to develop on, and as always .. with great powers comes great responsibility.

  • http://designwithcrackers.blogspot.com/ Thiago Cavalcanti

    If you’re okay buying a mansion just for the comfy beds and Victorian dinner plates (which you could get separately anyway), go for it man.

    Though I guess this makes perfect sense for those who are actually doing Web and Windows/Xbox work at the same time, there’s a whole bunch of people using .NET who don’t fit that profile and could be doing much better without it.

    It’s pretty much the same case of experienced web designers that “use” Dreamweaver but could be doing just fine with Notepad++, Kate or Vim.

    “Look guys! I’m making so much money that I can throw it away on expensive software which I’ll only be using a fraction of!”

    Sure you can do it, it won’t make your work worse (or better) anyway, just don’t expect everybody to believe that it’s okay to depend on a single company software quality, especially when that company has this impressively huge software collection which needs caring too…

  • http://www.roryf.co.uk Rory Fitzpatrick

    Great to see a high-profile piece about .NET in the web design world, and one that does a good job of explaining why it isn’t so bad!

    Tim Snadden made a good point about using .NET in a better way is swimming against the tide, but this is only in terms of what Microsoft says you should do with it. What the really good developers, the ones that realise WebForms are a load of pish, are doing is looking outside Microsoft for solutions. This has led to many good open source projects and communities promoting best practices that aren’t Microsoft-led.

    ASP.NET MVC could go a long way to fixing a lot of these issues, sadly I think it too will suffer from poor Microsoft tutorials in order to lower the barrier to entry.

  • http://twitter.com/charlesroper Charles Roper

    The problem with ASP.NET is not .NET itself, which is a well designed framework, of which C# is a well designed, if not perfect (I gather, from people who use it), language. You only have to look at the tags on StackOverflow to see that .NET, ASP.NET and C# occupy 3 of the 5 most popular tags there. It’s obviously popular, well-used, and well-liked.

    The problem is not really .NET itself, but Microsoft. Microsoft became so powerful and dominant that they have created a sort of ghetto. There’s the MS world, and then there’s everyone else. MS have intentionally tried to lock people in and “encourage” them to do things the MS way (e.g., webforms). Open standards, cross-polination and “biodiversity” are not in the MS DNA (although they have been making very slow moves to change this). It has always been in the MS interest to do things their own way. It’s the nature of their business.

    So in short, MS are like The Emprire, everyone else is like The Rebellion. And therein lies the rub. Microsoft has a serious image problem. No matter how good their tools, they are never going to gain the respect of the young, “I’m a Mac” demographic which makes up such a large proportion of web designers and developers. PHP is cheap, accessible and has a large volume of well loved “gateway” apps, such as WordPress, which in their own way are seen as “cool” and a little bit “rockstar”. It’s the same with Ruby and Ruby on Rails, only they have more respect from the computer science educated developers (PHP, it seems, is quite widely disliked and ridiculed as a pooly designed language by many well educated programmers).

    Simply put, .NET is uncool, and “uncool” to a web designer is like holy water to a vampire. Unless MS can radically change its image, it’s never going to stop getting ladled with hate by the youthful web design digerati. It’s probably much easier to just transcend such petty quibblings and get on with the task of building cool stuff.

    Final comment: I suggest listening to some <a href=”http://blog.stackoverflow.com/category/podcasts/”StackOverflow podcasts – they often discuss .NET (SoF was built using MVC.NET) and Joel and Jeff have quite a balanced outlook (well, apart from Jeff absolutely loathing PHP). I don’t use .NET myself (I use Ruby, Rails, PHP for the most part), but listening to the SoF podcast has given me a much better insight into it, and, dare I say it, a lot more respect.

    • http://twitter.com/charlesroper Charles Roper

      Crikey, my formatting got totally stripped out in my comment there. Sorry!

  • Keith Clark

    Personally, I think .net is a great platform but, as a “code by hand” style web developer, I have to admit I too am frustrated with the issues of view state, form ID’s (and field names), inline scripts etc.

    That said, there are ways round these issues. For example, you can do away with webforms and viewstate for some of your pages by overriding Page.DeterminePostBackMode() I do this to allow standard html forms/controls to use the PostBack architecture – this method also allows you to use multiple forms on one page – one of which can even be a .net webform.

    The problem with fixing this kind of issue is amount of time you need to invest in building your own .net framework layer which doesn’t ultimately offer any new features in terms of user experience or development process.

  • http://darrenstuart.com Darren

    asp.net 3.5 came with some great controls to remove all that unwanted markup from gridviews. I used to use repeaters a lot but now mainly its listviews with custom pagers. MVC.net fixed all of the markup issues thankfully.

    Another plus point for .net and the main reason I use it so much is the user management system out the box.

    I have worked places where I have seen aweful use of .net and this mainly down to .net 1.0 libraries being based on java being ported along instead of being rewritten. I think alot of the people that were in the .net camp and then jumped to ruby are prime examples of developers who were using c# like java.

    I myself currently develop on mac and win and mobiles and use .net, python, php and still find .net to be one the best performers out there (I am really enjoying coding in python too).

  • http://www.castus.co.uk Gary Hides

    One of the things that put me off about starting to use or at least dabble in .NET was the server costs – although I’m not too sure if I was totally off the mark with this, so maybe you can help.

    I was under the impression that when you make a .NET web app or use a .NET CMS you have to have MSSQL server and databases attached. Now most hosts charged a reasonable sum of money for this, and I was just wanting to check it out, thought I don’t really want to be paying an extra £300 a month just because I’m being inquisitive. The prices may have dropped now, but i think it was around that price when i checked it out.

    Was my assumption right, or could I have checked it out for much less?

    Cheers

  • http://www.expansionbroadcast.com Eric Masiello

    Just curious – can anyone recommend some good .NET books? I’ve been using .NET for a few years now but I feel, especially after reading this article, that there’s a lot I could be doing to make my code more efficient (e.g. relying less on viewstate). Any recommendations? (Preferably ones based on C# and not VB)

  • http://p163.sg/blog Dinu

    @paul: I didn’t know you were a programmer. Awesome stuff.

    I maintain both PHP and ASP.NET sites. However, It’s come to the point that I’m really frustrated with how .NET complicates things while trying to simplify them.

    The main reason I used .NET was for it’s easy integration with with MS Office and the ability to share classes with related Windows programs. However, developing for both PHP/Asp.NET takes it’s toll. So right now I’m trying to move everything to PHP.

  • http://www.vcarrer.com Vladimir

    ASP.NET is meant for building forms on web we use it only for intranets. Microsoft made fatal error by baning Classic ASP and introducing completely new philosophy in order to do drag and drop for everything so everyone could be a programmer. Fatal Error! All Classical ASP programmers migrated to PHP. Microsoft is now truing to fix the error by introducing ASP.NET MVC. ASP.NET is great for form based intranet system, web application but sucks for making simple clean web sites. Just open the HTML on any ASP.NET site and you will get the picture.

  • Karl

    I agree with Vladimir, MS completely lost the plot ditching Classic ASP and lost a lot of small dev houses to PHP due to the very little learning curve and similar model. I have been playing around with .Net for a few months now and my main gripes are the steep learning curve due to the sheer size of the framework and the architecture and the nasty html output, which drives me nuts especially when I spent so much time trying to re-educate myself to produce clean web pages with nice separation, using unobtrusive js and css. On the plus side I think MS are starting to make real progress with their support of jQuery and the new MVC model.

Headscape

Boagworld