Page 1 of 1

An Introduction to ASP.Net MVC

#1 coden4fun  Icon User is offline

  • if(coden4fun.NotTired){ ContinueToCode(); }
  • member icon

Reputation: 27
  • View blog
  • Posts: 696
  • Joined: 27-February 09

Post icon  Posted 03 January 2010 - 10:03 PM

Goal: An overview and introduction to the Microsoft ASP.Net MVC framework. The goal of this tutorial is to explain why you should build web applications using ASP.Net MVC. In this tutorial I will quickly discuss the architecture of an ASP.Net MVC application and how this achitecture enables you to write good software applications. I will provide you with the different parts of an MVC applications Models, Views, Controllers and how it is important to the architecture of the framework.

Obtaining MVC I'll be using Visual Studios 2010 which comes shipped with ASP.Net MVC 2, however, if you do not have this you can download it from the following place

What is ASP.Net MVC Microsoft ASP.Net MVC is the newest framework from Microsoft for building web applications. The framework was built to use pattern-based software development as MVC is a design pattern. The framework was also designed to provide UnitTesting. I won't mention it here, or write a UnitTest tutorial here, but if you don't know what it is Bing it.

MVC is part of the ASP.Net framework. The ASP.Net framework is Microsoft's framework for building web applications. Microsoft has 2 frameworks built on top of this framework, which is ASP.Net MVC (which we're discussing) and the one many are familiar with ASP.Net Web Forms. ASP.Net MVC is an alternate but not a replacement for Web Forms.

As I mentioned ASP.Net MVC was designed in mind to keep strong design patterns in use. This is to make the software you make more extensible, longer lasting. The big main design pattern that is used is MVC. Model-View Controller. This is nothing new to open-source languages like Ruby on Rails (which I believe was the first web dev language to use MVC).

Architecture of ASP.Net MVC

Model - An MVC Model contains all of an application's logic that is contained in a view or controller. The model includes all of an applications validation logic, business logic, and DAL. The model contains model classes that model objects in the applications domain.[/li]
View - HTML markup and view logic (could be client side js in an HTML view page)
Controllers Contains the control, or flow logic of the application. The controller communicates to both the models and the views to make sure the application executes smoothly

Sample Project
OK, now let's get our hands dirty. Enough software design pattern and architecture lingo let's get to the code, right? If you're running VS2010 on a virtual machine like myself then you can just boot it up in either .Net3.5 or 4.0 and go to File > New Project > ASP.Net MVC give it a name I named it MVCApplication.

Posted Image

After you have created the project expand the views folder, and the controller folder and you'll get the following image below

Posted Image

The structure of your Project
As I discussed earlier you have your Models, Views, and Controllers. If you look at the image above you can see that the views are nothing more than .aspx pages, because the MVC is on top of the ASP.Net framework.

The contorllers look a bit different they all end with InserNameHereController and that is for good purposes. If you want to create a controller the communicator between the Models and the Views then you have to create a new Controller and make sure that not only does your class end in Controller but that it also inherits Controller. However, this is really easy to understand use with the ASP.Net MVC framework because MS has made nice tools in VS like Create > New > Controller when you right click on the Controller folder.

Let's look at a simple example of a small web application that comes standard when you create a new ASP.Net MVC application and see what it is doing.

some code from the HomeController.cs
 [HandleError]
	public class HomeController : Controller
	{
		public ActionResult Index()
		{
			ViewData["Message"] = "Welcome to ASP.NET MVC!";

			return View();
		}

		public ActionResult About()
		{
			return View();
		}



In this controller we're creating 2 new ActionResults one named Index, and one named About. These are nothing more than calling a View titling their names. Inside Index we have a ViewData, which is nothing more than a way for a controller to pass data to the view. The viewData is of type string, which you can see what is says.

Inside the Index.aspx
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
	Home Page
</asp:Content>

<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
	<h2><%= Html.Encode(ViewData["Message"]) %></h2>
	<p>
		To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
	</p>
</asp:Content>



Inside the view of Index you see that there is that ViewData["Message"] using the Html.Encode is just a way of accessing the ViewData amongst other things, and I won't get to technical with this tutorial, since we're just setting up the framework, and seeing how the framework is layed out.

If you press Ctl + F5 (run without debugging) You'll get a similar page to the following below.

Posted Image

Is This A Good Question/Topic? 0
  • +

Page 1 of 1