Introducing WebMatrix - Part 2
 
Published: 06 Jul 2010
Unedited - Community Contributed
Abstract
In this second part of the article, Scott examines the development of a simple ASP.NET application using WebMatrix including the steps required for database connectity and deploying the application with the help of source codes and screenshots.
by Scott Guthrie
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 27577/ 58

Introduction

Republished with Permission - Original Article

The first part of the article discussed the important aspects related to WebMatrix tool. In this part, we will examine how to build a simple ASP.NET application using WebMatrix including the process of database connectivity and usage of HTML Helpers.

Building our First Simple ASP.NET Web Page

Let's build a simple page that lists out some content we are storing in a database. 

If you are a professional developer who has spent years with .NET you will likely look at the below steps and think – this scenario is so basic - you need to understand so much more than just this to build a "real" application. What about encapsulated business logic, data access layers, ORMs, etc?  Well, if you are building a critical business application that you want to be maintainable for years then you do need to understand and think about these scenarios.

Imagine, though, that you are trying to teach a friend or one of your children how to build their first simple application – and they are new to programming.  Variables, if-statements, loops, and plain old HTML are still concepts they are likely grappling with. Classes and objects are concepts they haven’t even heard of yet. Helping them get a scenario like below up and running quickly (without requiring them to master lots of new concepts and steps) will make it much more likely that they’ll be successful – and hopefully cause them to want to continue to learn more.

One of the things we are trying to-do with WebMatrix is reach an audience who might eventually be able to be advanced VS/.NET developers – but who find the first learning step today too daunting, and who struggle to get started. 

We'll start by adding some HTML content to our page.  ASP.NET Web Pages typically start as just HTML files.  For this sample we'll just add a static list to the page:

Just like with our previous scenario, IIS Express has been automatically configured to run the project we are editing – and we do not need to configure or setup anything for our web-server to run our site.

We can press “F12” or use the “Run” button in the Ribbon toolbar to launch it in the browser.  As you’d expect, this will bring up a simple static page of our movies:

image

Working with Data

Pretty basic so far.  Let’s now convert this page to use a database, and make the movie listing dynamic instead of having it just be a static list.

Create a Database

We’ll start by clicking the “Databases” tab within the left-hand navigation bar of WebMatrix.  This will bring up a simple database editor:

SQL Server Compact Edition ships with WebMatrix – and so is always available to use within projects.  Because it can be embedded within an application, it can also be easily copied and used in a remote hosting environment (no extra deployment or setup steps required – just publish up the database file with FTP or Web Deploy and you are good to go).

Note: In addition to supporting SQL CE, the WebMatrix database tools below also work against SQL Express, SQL Server, as well as with MySQL. 

We can create a new SQL CE database by clicking the “Add a Database to your site” link (either in the center of the screen or by using the “New Database” icon at the top in the ribbon).  This will add a “FirstSite.sdf” database file under an \App_Data directory within our application directory. 

We can then click the "New Table" icon within the Ribbon to create a new table to store our movie data.  We can use the "New Column" button in the Ribbon to add three columns to the table – Id, Name and Year.

Note: for the first beta you have to use the property grid editor at the bottom of the screen to configure the columns – a richer database editing experience will show up in the next beta. 

We’ll make Id the primary key by setting the "Is Primary Key" property to true:

We'll then hit "save" and name the table "Movies". Once we do this it will show up under our Tables node on the left hand side. 

Let’s then click the "Data" icon on the ribbon to edit the data in the table we just created, and add a few rows of movie data to it:

And now we have a database, with a table, with some movie data we can use in it.

Using our Database within an ASP.NET Web Page

ASP.NET Web Pages can use any .NET API or VB/C# language feature.  This means you can use the full power of .NET within any Web site or application built with it.  WebMatrix also includes some additional .NET libraries and helpers that you can optionally take advantage of for common tasks.

One of these helpers is a simple database API that allows you to write SQL code against a database.  Let’s use it within our page to query our new Movies table and retrieve and display all of the movies within it.  To-do this we’ll go back to the Files tab in WebMatrix, and add the below code to our Index.cshtml file:

As you can see – the page is conceptually pretty simple (and doesn’t require understanding any deep object-oriented concepts).  We have two lines of code at the top of the file.

The first line of code opens the database.  Database.Open() first looks to see if there is a connection-string named “FirstSite” in a web.config file – and if so will connect and use that as the database (note: right now we do not have any web.config file at all).  Alternatively, it looks in the \App_Data folder for a SQL Express database file named “FirstSite.mdf” or a SQL Compact database file name “FirstSite.sdf”.  If it finds either it will open it.  The second line of code performs a query against the database and retrieves all of the Movies within it.  Database.Query() returns back an dynamic list – where each dynamic object in the list is shaped based on the SQL query performed.

We then have a foreach loop within our <ul> statement, which simply iterates over the movies collection, and outputs each name as a <li> element.  Because movies is a collection of dynamic objects, we can write @movies.Name instead of having to write movies[“Name”].

When we re-run the page (or just hit refresh on it in the browser) and do a “view source” on the HTML returned to the client, we’ll see the following:

image

The list of movies above is now coming out of our database and is dynamic.

Adding a Simple Filter Clause

One last step we can do to make our application a little more dynamic is to add simple support to filter the list of movies based on a querystring parameter that is passed in. 

We can do this by updating our Index.cshtml file to have a little extra code:

Above we added a line of code to retrieve a “year” querystring parameter from the Request object.  We are taking advantage of a new “AsInt()” extension helper method that comes with WebMatrix.  This helper returns either the value as an integer, or if it is null returns zero.  We then modified our SELECT query to take a WHERE parameter as an argument.  The syntax we are using ensures that we cannot be hit with a SQL injection attack.

Lastly, we added an if statement inside our <h1> which will append a (post 1975) message to the <h1> if a year filter is specified.  And now when we run the page again we will see all movies by default:

image

And we can optionally pass a “year” querystring parameter to show only those movies after that date:

image

Other Useful Web Helpers

I used the Database helper library that ships with WebMatrix in my simple movie listing sample above. 

WebMatrix also ships with other useful web helpers that you can take advantage of.  We’ll support these helpers not just within ASP.NET Web Pages – but also within ASP.NET MVC and ASP.NET Web Forms applications.  For example, to embed a live twitter search panel within your application you can write code like below to search tweets:

This will then display a live twitter feed of tweets that mention “scottgu”:

image

Other useful built-in helpers include ones to integrate with Facebook and Google Analytics, Create and Integrate Captchas and Gravitars, perform server-side dynamic charts (using the new Chart capabilities built-into ASP.NET 4), and more.

All of these helpers will be available for use not only within ASP.NET Web Pages, but also in ASP.NET Web Forms and ASP.NET MVC applications.

Easy Deployment

Once we are done building our custom site, we can deploy it just like we did with BlogEngine.NET.  All we need to do is click the “Publish” button within WebMatrix, select a remote hosting provider, and our simple application will be live on the Internet.

Open in Visual Studio

Projects created with WebMatrix can also be opened within Visual Studio 2010 and Visual Web Developer 2010 Express (which is free).  These tools provide an even richer set of features for web development, and a work environment more focused on professional development.  WebMatrix projects can be opened within Visual Studio simply by clicking the “Visual Studio” icon on the top-right of Ribbon UI:

This will launch VS 2010 or Visual Web Developer 2010 Express, and open it to edit the current Web site that is open within WebMatrix.  We’ll be shipping an update to VS 2010/VWD 2010 in the future that adds editor and project-system support for IIS Express, SQL CE, and the new Razor syntax.

How to Learn More

Click here to learn more about WebMatrix.  An early beta of WebMatrix can now be downloaded here

You can read online tutorials and watch videos about WebMatrix by visiting the www.asp.net web-site.  Today’s beta is a first preview of a lot of this technology, and so the documentation and samples will continue to be refined in the weeks and months ahead.  We will also obviously be refining the feature-set based on your feedback and input.

Summary

IIS Express, SQL CE and the new ASP.NET “Razor” syntax bring with them a ton of improvements and capabilities for professional developers using Visual Studio, ASP.NET Web Forms and ASP.NET MVC.

We think WebMatrix will be able to take advantage of these technologies to facilitate a simplified web development workload that is useful beyond professional development scenarios – and which enables even more developers to be able to learn and take advantage of ASP.NET for a wider variety of scenarios on the web.

Hope this helps,

Scott

Follow Scott at: twitter.com/scottgu

Resources



User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-28 8:20:40 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search