LOGIC Library

This site is the Logic involvement in sharing expertise and skills acquired in daily work. The goal is to create a solid knowledge base and share best practices in software development and systems management.

More info about us can be found on logicsistemi.it.

ASP.NET MVC 4 tutorial for web developers - part 3

We have a working example of a .Net MVC 4 application and it's now time to take a look at how this paradigm, in the Microsoft version, works.

After a fast description of the MVC paradigm we'll analyze how it is implemented in the test application.

MVC explained

MVC paradigm is based on 3 different object types:

  • The model: it is the part of code responsible of data management. This is the part that manages the connection with the data source (i.e. a SQL DB) and that should implement the business logic related to data validation and data manipulation
  • The view: it displays (format) the information to the user. This information can be in a raw form, such as a JSON or a CSV file, or in a format that decorates data with user controls that allow some interaction, such as an HTML page
  • The controller: this is the part of code that interprets user requests. In a web application it analyzes request parameters and triggers the right model action and the right view to display: it's the glue between view and model.

In a MVC web application the standard code flow is the following:

  • Routing (we will talk about it later) map the requested URL to the right resource (a physical file, a controller or other)
  • The controller analyzes the parameters that come with the user request and determines the action to perform.
  • If the action requires some interaction with data, a method of the right model is called
  • The action passes model data to a view that generates an HTML page or some other kind of output. This output is sent back to the user.
  • This page contains controls that generate other requests

The MVC approach for web applications leads to request URLs that are not "user friendly". These URLs must contains information about the controller and the action to display and, using a PHP example, can be in the following form:

http://www.mydomain.com/index.php?controller=hello&action=list

To have simpler URLs an MVC web application normally make use of the web server rewriting feature and allows to write the same request in the following way:

http://www.mydomain.com/hello/list

If you launch again the test application you will find URLs in the latter format. So, if you click on the About link in the menu you request the about action of the home controller.

Where's my index.php?

After the explanation of the different objects that build up MVC, it's now time to take a look at the code generated for the base application.

Come back to Visual Studio and look at the files in the solution explorer.

It's easy to locate controllers, models and views folders. These are the folders where the corresponding objects are stored. But where is the page that corresponds to our index.php? There's no default.aspx page, so there's no "entry point" for the application. It's now time to talk about a feature called "routing".

In ASP.NET MVC "routing" is a feature that combines togheter URL rewriting and URL parsing done, in PHP, by the index.php file. Through routing a URL that doesn't correspond to a phisical file, is mapped to a resource that, normally, is a controller action.

Moreover, routing is bidirectional: your URL is not rewritten, but it is the real name of your resource. All your code will always sees this URL and not, as happens in a PHP application, the rewritten URL.

Defining routing

Global.asax file contains event handlers related to the application and session lifetime. This events can set parameters that modify every request behavior. If you open it you will find the Application_start event handler with the following content:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    WebApiConfig.Register(GlobalConfiguration.Configuration);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
    AuthConfig.RegisterAuth();
}

This method defines standard settings to manage different aspect of your application. In particular you'll find the RegisterRoutes method that set the standard routing mechanism based on the following routing path:

routes.MapRoute("simple", "{controller}/{action}/{id}");

This path is completed with default values that map to the "Home" controller and to the "Index" action. This means that if you call only the URL http://www.mydomain.com this is treated as http://www.mydomain.com/Home/Index.

Routing customisation is beyond the purpose of this tutorial, but you can find more about it in this article.

It's time to analyze the code...

Yes, I think the theory is enough for now! In the next post we will analyze the Home controller and the first view. Starting from there we will create the first page of our application.

See you soon!

Add comment


Security code
Refresh