Categories
Uncategorized

Object Oriented HTML in ASP.NET – Part 01, Introduction

Getting data on the web can be either be met with trepidation or can be fairly straightforward. I am going to show an approach to server-side web development that is compact and efficient. What is shown here involves work of about 6 – 12 hours a day between March 30th and April 2nd 2015. Just under 4 days. The concept is object-oriented html generation to get data on the web using ASP.NET.

Getting Back Into ASP.NET

The last time I wrote any serious ASP.NET code before March 30, 2015 was sometime in February 2013. A good two-year gap. I did have a brief interlude of about a month in 2014 using drag and drop, WYSIWYG style Web Forms, but nothing major. I was interested to try a small demo project to see to what extent, my multi-year review of native code development had on my approach to web development. In the process, this was an opportunity to show an A to Z process involving several aspects of getting data on the web.

Web Development Background

I have created web sites and applications every year since 1998 and took a break from 2013 to present, current demo notwithstanding. The forerunner to ASP.NET, Classic ASP, was the first Microsoft-based web platform which I first used in 1999. What began as small exploratory endeavors blossomed into full professional grade application by late 2000. My knowledge of .NET also began in Oct. 2000 and I first adopted ASP.NET in 2003 after about a year creating Windows Services and back-end data processing solutions in C#.

ASP.NET Technology

Microsoft created a technology called ASP.NET for defining web applications. It is a single technology with multiple ways to use it. The Web Forms way was the primary approached advocated in the first years of ASP.NET’s existence. The MVC framework is the approach most favored today. Then of course we have ASP.NET web services and many other things. The bottom line is that ASP.NET is Microsoft’s go to technology for web code that runs on computer servers.

HTML Knowledge for ASP.NET

One of the problems of ASP.NET can be that it can be more Microsoft than Web. What that means is if you have learned HTML, CSS, JavaScript, and understand those things at either a beginner or expert level, you may be surprised when you use ASP.NET. Some forms of ASP.NET attempts to make web development easier by defining it in a language that is not pure HTML. I overlook such things in professional projects since the Microsoft approach results in far less time spent defining a web solution that is robust and thorough. Some situations however can benefit from an approach that is closer to pure HTML definition but with the benefits of structured coding.

Object Oriented HTML

The series I am presenting here, across 7 articles, shows another approach using ASP.NET to get closer to the pure HTML you may have learned and use the structured nature of .NET to achieve a solution that maps more closely HTML authoring. The benefit is less surprises over how the HTML markup is generated. That translates into greater control over how the page looks and functions. Along the way, I will integrate a database in what I call a classic fashion that retrieves data fast under the .NET umbrella.

Technologies and Audience

Mentioning ASP.NET can imply use of Microsoft Windows and Visual Studio. That is not the case here. Instead, I will be using the Linux platform to accomplish the objectives in this series. The audience for this article is everyone since I basically outline the steps from beginning to end. You only need literate knowledge of Microsoft C#, .NET, and the ability to use a mouse and keyboard. All the ASP.NET and .NET Code shown in this article is applicable to a Microsoft Windows environment. The difference is this code is more cross-platform in terms of .NET compatible servers it can run on.

Technology Mapping to Microsoft Equivalents

If you are accustomed to Microsoft ASP.NET on Windows I have a map of technologies to show how the technologies in this article are related in purpose. This map can help clarify what is used and why. The left side is the traditional Microsoft technology and the right shows the Linux-based equivalent.

Microsoft

Linux

Windows Ubuntu
Visual Studio Gedit and Vi
SQL Server Postgres
Hyper-V KVM and Qemu
Remote Desktop SSH
IIS Apache
.NET Runtime Mono Runtime
ASP.NET Web Forms/MVC ASP.NET Http Handler
SQLClient Data Provider PostgreSQL Data Provider

Starting with Apache

The web server software is Apache. It is going to take requests for ASP.NET pages and forward them to the Mono Runtime. Apache is the bridge between the web browser and the execution of the code. Out of the box, it knows nothing about the Mono Runtime or .NET. A default install of Apache on Ubuntu may appear as follows.

oo_html_001_apache_configuration

Then we have the home page for the Ubuntu install of Apache under the html directory.

oo_html_002_apache_default_page

Configuring Apache for Running ASP.NET

After you have installed the complete Mono technology, you will be able to run ASP.NET in theory. I did not find that to be the case at first. It took me roughly 10 hours of trial and error. Apache was the first web server I had ever used back in 1998 and that was the last time I saw it. It was time to ramp back up. The configuration files are my souvenirs from the journey to get ASP.NET running on Apache using Mono.

oo_html_003_apache_config_files

Later, I started over and decided there must be a better way. The benefit of delving deeply into the materials on configuration and executing them hands-on was that I was able to determine that much of the documentation is out of date. The fastest way to get Mono working on Ubuntu is to go with auto hosting as described on the Ubuntu Wiki and put your virtual directories under /var/www/. I was trying to do things the Microsoft IIS way and that was a 10-hour loss that reminded me the web development technology I am using may have originated from Microsoft but the operational approach is firmly rooted in Unix philosophy. The following is the result of this realization.

oo_html_004_apache_index_document_showing_top_level_directories

Initial Run of the ASP.NET Application

I am ready to test the code when I see the following. After going through a few log files in /var/log/apache, I decided I needed to take a break.

oo_html_005_503_message_first_attempt_to_visit_oohtml

Triage with Mono Develop

I came back to the solution which comprised a single web page and decided to add a Web.config file and a Global.asax file with appropriate code behind. These are automatically created for you by Visual Studio. While I did not have Visual Studio, I did have Mono Develop as a means to generate a Mono-friendly version of Web.config and a skeleton of Global.asax and Global.asax.cs. That would be the only time I would need to use Mono Develop. I recompiled the solution on the command-line, posted a new copy of the built executable, associated files and ran a new test.

oo_html_006_test_message_home_ashx

Building ASP.NET on the Command-Line

I was curious if a project could be done, start to finish, creating an ASP.NET solution with nothing more than the command-line. I would find out in this project. Most of my experience with ASP.NET was with Microsoft Visual Studio which creates many things for you automatically. As someone who has studied ASP.NET since late 2002, applying it since 2003, I wondered if ASP.NET could be narrowed down to the essentials. The following build script is the answer to that question.

oo_html_007_build_script_to_produce_web_executable

C# Code for Test Output

Now, I can run ASP.NET on Apache using Mono. I skipped over the parts about configuring firewall rules and setting permissions on directories and so on. If you do an out-of-the-box install of Ubuntu, you won’t need much of that. I am operating a configuration that needed a little more tuning before I could reach this point. What you see now is the full sum of ASP.NET code to output the words Test to the web browser.

oo_html_008_minimal_web_code

The Web Page

Apache looks for a web page. A file that has plain text describing a web page. An html page will simply be lifted from the file system and sent directly to the web browser. The web browser will then read that html data and present words, colors, graphics, etc. Our solution is to create HTML on the fly in a piece of code that is in an executable. Apache needs a way to load that code so it can be put this purpose. The file format below with an appropriate file extension is the trigger Apache needs. You will notice that the value of the Class field matches the name of the class in the previous section. File pairs of this kind is all we need to get started.

oo_html_009_actual_web_page

Next

The discussion continues with a focus on technology in Part 2.

Leave a comment