Friday, October 3, 2008

Programming ASP.NET with Notepad.exe

My first experience with web applications was with JSP pages, and my first application was like this:
<%@ page contentType="text/html;charset=WINDOWS-1252"%>
<HTML>
<BODY>
<%="Hello World"%>
</BODY>
</HTML>
This application was written in Notepad and deployed on the Apache Tomcat Server. That meant, at least for me, you don't need a compiler do develop something simple and dynamic. That was something cool.

Today I was deploying an ASP.NET Application and noticed that all the ASPX files have only this text:

This is a marker file generated by the precompilation tool, and should not be deleted!

and I was wondering.... “Can you program an ASP.NET Application without Visual Studio, using any text editor and deploy it manually without any compilation?”.

So I tried.
I created a new folder.
I mapped the folder on the IIS server.
I created a new aspx file in that folder with the content:
<%@ Page %>
<HTML>
<BODY>
<%="Hello World!"%>
</BODY>
</HTML>
I opened the web browser and browsed the http://localhost/MyFolder/Default.aspx and I got the Hello World! message.
To make it a little more complicated a modified the Default.aspx page to do something dynamic, something like a calculation. I think 1 + 1 will do:
<%@ Page %>
<HTML>
<BODY>
<%="1+1=" + (1+1).ToString()%>
</BODY>
</HTML>
and the obvious result displayed was 1+1=3 :).

So you are able to write ASP.NET applications using only the Notepad and an IIS server.

... but you are in .net, not in Java where the default language (and the only one) is Java. Witch language is the default for this simple page? The fastest way I could find out is to write something in c# language and see the result.

The new Default.aspx
<%@ Page %>
<HTML>
<BODY>
<% for(int i=0 ; i<5 ; i++) { } %>
</BODY>
</HTML>
The result:

Compiler Error Message: BC30084: 'For' must end with a matching 'Next'.

Looking in the Detailed Compiler Output, I saw that the default compiler for the asp.net applications is C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\vbc.exe (visual basic compiler).
Adding the language attribute to the page directive and pointing correctly to the language you use it compiles OK.

Wednesday, October 1, 2008

Decorating the return!

When I discovered the .Net there ware some attributes, and because I have a little more practical tendencies (less theoretical ones) it never caught my attention that I can use an attribute on a return value of a method. Looking one day at the AttributeTargets enum and seeing the ReturnValue value I realize that I didn’t knew how you can decorate a return value. After I searched on Google an example of it, this is what o found:
[MethodAtr(Params...)]
[return: ReturnAtr(Params...)]
public SomeType MyMethod(
[ParamAtr(Params...)]
SomeOtherType myParam)
{
return someValue;
}
Where [return: ReturnAtr(Params...)] is the way of decorating a return value.

ASP.NET Dynamic Data and other ORMs

Searching to see if I could use with the ASP.NET Dynamic Data some other ORMs besides the EF and LINQ to SQL, I stepped on ValidDataContextType method from the System.Web.DynamicData.ModelProviders.SchemaCreator. The method body looks like this and I think it provides enough information about the extending to other ORMs:
public virtual bool ValidDataContextType(Type contextType){
if (!IsDataContext(contextType)){
return IsObjectContext(contextType);
}
return true;
}
The only valid DataContexts are System.Data.Linq.DataContext or System.Data.Objects.ObjectContext. So if you want to extend the ASP.NET Dynameic Data you can do this by implementing a LINQ2 or EF provider to some database. Here, Frans Bouma gives more references aboute this issue.