When Written: Aug 2010
Regular followers of Microsoft’s evolution of web development will know that for years they have been struggling to separate code from design and it could be argued that they have achieved this with ASP .NET. So why, I ask myself, would they produce a new language that seems to fly in the face of this trend? The reason for this move I am told is not to replace ASP .NET but to provide an up-to-date alternative to the old ASP and PHP. This new language Razor allows code to be put within the HTML page but this time with the extension of either vbhtml for Visual basic code or .cshtml for C# based code. Now, whilst coding in Razor does not allow the level of flexibility of .NET it can achieve a lot in just a few lines of code. For example if you want to show a twitter feed on your web page then in Razor all you need is :
@Twitter.Search("#bakery", caption: "#bakery")
Notice the use of the @ to show the web server that what follows is Razor code. To connect to a database then something like:
@var db = Database.Open("bakery")
To make the connection, and then to read the resultant recordset:
@foreach (var p in db.Query("SELECT * FROM PRODUCTS")) {
<div class="product">
<h3>@p.Name</h3>
<img src="@Href("~/Images/"+ p.ImageName)" alt="@p.Name"/>
<p>@p.Description</p>
</div>
}
Is all that is needed to display a heading, image and description for each item in the database. As a certain meerkat would say ‘Simple!’. Easy stuff and the code is still reasonably easy to follow. But currently there is no tool that gives intellisense for Razor and whilst the language set is currently very small, providing a development environment which is something more than a pretty looking notepad would be useful. So why this move? Why produce a web application language which goes back to the bad old days of ASP with poor debugging capabilities?
The simple answer is because programming then was a lot easier, particularly to get started. That is part of the charm of PHP, you can get going with nothing more than a text editor and start turning out dynamic web pages. You start down on this route and before you know it you are producing quite complicated websites and there is little desire to move to a more complex programming structure like J2EE or ASP .NET unless you are having to develop applications with teams of programmers where the forced structure and the more advanced tools really start to pay off. Whether Microsoft will, like they have done before, ‘play’ with Razor and then ditch it in favour of something else is yet to be seen.
I suspect that Razor is an attempt to win converts to web development who are starting out and put off by the complexities of ASP .NET development. Personally for these people I would suggest that they are much better served by going down the PHP route as at least we know this will continue to be supported by more than one manufacturer. Microsoft has promised intellisense for Razor for Visual Studio, a task that is not too great and so should happen, as long they think the life span of Razor makes it worthwhile (http://www.asp.net/webmatrix/tutorials/2-introduction-to-asp-net-web-programming-using-the-razor-syntax ).
However, WebMatrix has a few little tricks under the bonnet other than a new programming language. There is a new version of SQL Server Compact which has been optimised for the web and which can be deployed simply by file transfer so implementing small database driven web sites becomes easier, and with this encouraging a new generation of beginner web programmers there will probably an increase in the number of sites that are vulnerable to SQL injection attacks. A particularly interesting new feature that gets installed with WebMatrix is IIS Express (http://weblogs.asp.net/scottgu/archive/2010/06/28/introducing-iis-express.aspx ), which is a simple to install full version of IIS so that all features of your application like SSL and URL re-writing can be tested, something which is currently impossible under the local web server called ‘ Cassini’ which Visual Studio uses by default.
WebMatrix – A little boring but some interesting stuff under the hood]
Now if you need to test such things in your web application under Visual Studio you can always switch it to use the full version of IIS although there are problems with this route, one being that IIS can be a little resource hungry and requires administrator rights to install on a machine. This may not be allowed by the domain group policies, and also a lot of networks will not allow web servers to be installed on them. IIS 7 will not install on XP whereas IISExpress will, and supports the full features and extensibility of IIS7, and if you are not totally convinced that this is the way to go and your networks security policy allows it then you can install IIS and IISExpress alongside on the same machine, just to be sure. There will be a patch that will allow the use of Visual Studio with IIS Express in the near future according to Scott Guthrie of Microsoft.
Article by: Mark Newton
Published in: Mark Newton