AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=1538&pId=-1
Building a Simple Blog Engine with ASP.NET MVC and LINQ - Part 1
page
by Keyvan Nayyeri
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 153483/ 252

Introduction

A few weeks ago Microsoft released the first CTP of ASP.NET 3.5 Extensions which includes the first public version of ASP.NET MVC Framework. There have been many good resources about this MVC Framework on the web as tutorials, blog posts and web casts.

I want to write a set of articles about ASP.NET MVC Framework to build a simple blogging engine based on MVC and LINQ in .NET 3.5 and show you all the steps in detail then extend the discussion to some more-in-depth topics. I have not written this application completely and will write it in parallel with these articles.

Such a sample application is provided by ASP.NET MVC toolkit, but there is not a description about it yet. But why I am doing this while there are some other tutorials with the same approach? In my opinion, the learning curve of this MVC Framework consists of a workflow progress of some steps that should be followed and the major thing is this, building this simple blogging engine and describing its process would be a very good example for ASP.NET MVC Framework.

So here is the first part of this series (I want to finish them in a few weeks) and it is an introduction with some basic information.

What is MVC?

Yes, what is that?! Others have described this better than me but I repeat it in my words.

Model View Controller (MVC) is a methodology to build an application based on the idea of dividing the implementation into three roles: Model, View and Controller.

Let us take a second look at the above sentence. From the above sentence we believe that MVC is a methodology not a technology, so it has been used before Microsoft adapts it to ASP.NET. I persisted on this because saw some guys who thought it was a technology that is designed for ASP.NET specifically.

I also need to introduce the three roles in a nutshell:

·         Model: This is the role to maintain the state. Usually these are classes that represent data in a database.

·         View: The second role displays the data in the user interface to end users. This may be a set of user interface elements like TextBoxes, editors and buttons.

·         Controller: The last role is responsible to interact with user inputs and handling it. In fact, controller is where you implement the actual logic to handle what the user has asked for.

The MVC has become a very good methodology to design applications that rely on data interactions. The most important benefit of MVC is the ability to unit test the application easily because you can unit test an MVC application via its controllers and apply a TDD workflow (Red-Green-Refactor) easily.

Figure 1 shows the structure of MVC pattern where the model is independent from the controller and view so this enables the testability of the model independently from what controller and view are. On the other hand, there is a separation between model, view and controller that lets developers test their MVC applications easily. In fact, separation of view as the user interface element and controller and model is important because it simplifies the testing process.

Figure 1: MVC structure

I do not discuss more about the MVC and just refer you to Scott Guthrie's post about it and its description on Wikipedia that would be sufficient to get you started.

What is ASP.NET MVC Framework?

The ASP.NET MVC Framework is the Microsoft's adoption of MVC for its ASP.NET technology for version 3.5 where it provides features required to build a web application based on MVC.

ASP.NET MVC Framework is currently under construction and only one public release is available for download as a part of ASP.NET 3.5 Extensions CTP.

Once you download and install this package, you can get the benefit of different pieces of ASP.NET 3.5 Extensions and it automatically installs some project templates for you on Visual Studio 2008.

Along this download package, there is an MVC toolkit package available for download that provides some helpful tools for your MVC development. I may use them in my posts as well.

Basics of KBlog

For this article series, I am going to build a simple sample blogging tool that I call KBlog. This blogging engine will be built on top of some considerations that will be described here.

This blogging engine supports blog posts, comments and categories. A blog post can have only one category and as many comments as possible. This blog engine is for single users only and has some URL patterns for public and private pages. Everything will be referred by its ID and KBlog does not support post names as a part of URL just to make the application simpler.

I will use SQL Express for my database and for the first steps. I fill this database manually and after finishing my discussion with public pages, I will jump into details of administration pages where the user can enter data.

Create the Project

Obviously, the first step is to create a project in Visual Studio. Under Web category, you can choose "ASP.NET MVC Web Application" project item which should be available after installing ASP.NET 3.5 Extensions CTP. I name my project KBlog (Figure 2).

Figure 2: Create a new ASP.NET MVC Web Application project

After this, Visual Studio generates a project for you with the pre-defined template and structure.

Getting Started

At first glance, Visual Studio generates an ASP.NET web application project for you with a simple structure.

Figure 3: ASP.NET MVC solution structure

Taking a look at this project, you believe that there are some references for the project and one important reference is to System.Web.Extensions assembly. There are also four folders available including Content, Controllers, Models and Views.

·         Content folder is just for a default CSS stylesheet file for default look and feel of the site.

·         Controllers folder is responsible to hold controller classes.

·         Models folder is responsible to hold model classes.

·         Views folder is responsible for holding view files and contains some folders that include ASPX and master files.

As you see, Controllers, Models and Views are more important for us here. While they are generated to hold controllers, models and views, this is not a rule and you can replace them with your own structure.

Beside these folders, there are some well-known ASP.NET files that of course have some extra definitions that I will describe later.

There is a point to mention here about Default.aspx file and as mentioned in the content of this file, you should not remove the file to let IIS activate MVC for the application when the user navigates to this page.

That is enough for now. Step-by-step, I am going to add my implementation to this project in the upcoming posts.

Further Reading

Here is a list of some good blogs that you can use for further reading about ASP.NET MVC framework and they already contain excellent information about ASP.NET MVC. This technology is completely new in ASP.NET so there is a hope to see many more resources in the future, but fortunately MVC has become popular among developers and we saw great reactions against it in these a few weeks.

·         Scott Guthrie

·         Scott Hanselman

·         Phil Haack

·         Rob Conery

·         Brad Abrams

Summary

In the first part of my article series about new ASP.NET MVC framework, I introduced you to the MVC pattern, fundamentals of Model View Controller and ASP.NET MVC framework as well as basic information about the simple blogging engines that I am going to write in this series. I also covered the structure of MVC solutions in ASP.NET MVC.

In the future parts, I will build this blogging engine step-by-step and will show you some principles and techniques about the ASP.NET MVC framework.


Product Spotlight
Product Spotlight 

©Copyright 1998-2017 ASPAlliance.com  |  Page Processed at 2017-11-24 7:00:14 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search