Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Basic Understanding On ASP.NET MVC 4

0.00/5 (No votes)
4 May 2013 2  
Basic Understanding On ASP.NET MVC 4

 

Introduction to ASP.NET MVC 4

You can develop an ASP.NET MVC 4 web application by using the Microsoft Visual Studio Express 2012 or Visual Web Developer 2010 Express Service Pack 1.But Visual Studio 2012 is recommended. For Visual Studio 2010 you must install the following components which are mentioned below

1) Visual Studio Web Developer Express SP1 prerequisites: -http://www.microsoft.com/web/gallery/install.aspx?appid=VWD2010SP1Pack

2) WPI installer for ASP.NET MVC 4:- http://go.microsoft.com/fwlink/?LinkId=243392

ASP.NET MVC 4 is a framework for building scalable, standards-based web applications using well-established design patterns and the power of ASP.NET and the .NET Framework. 

What is MVC?

ASP.NET MVC helps us to develop powerful and pattern-based dynamic websites that enables a clean separation of concerns and also gives you a full control on a markup. First time it was implemented by Trygve Reenskaug at 1979 and it was implemented on Smalltalk at Xerox labs. Also it includes many features that help us to develop a sophisticated and modern web application. 
Here M stands for Model, V stands for View and C stands for controller. 

 

Release History of ASP.NET MVC 



Controller: - The controller is like a traffic cop.Whenever user requests any resource it first goes to the controller. Controller than interact with both the Model and View. 

Model: - The model is responsible for the data for the application and also it creates data for the view. It handles data processing and database works part.  

View: - It is a presentation layer i.e. it shows the data to the users.  

Advantages of MVC 

 In 3-tier architecture, Separation of concern i.e. independent of UI and Business layer. So Business logic is independent and can be used from different presentation layers.  

Disadvantages of MVC  

 In a View, managing a state (i.e. View state) is a painful process. But it can be achieved by using JavaScript/Ajax in the view page. 

 What is View Engine?  

The view engine is responsible for creating HTML from your views. Views are usually some kind of mix-up of HTML and a programming language. The pattern behind most of these is called two-step view.

 

If you have a Web application with many pages, you often want a consistent look and organization to the site. If every page looks different, you end up with a site that users find confusing. You may also want to make global changes to the appearance of the site easily, but common approaches using Template View (350) or Transform View (361) make this difficult because presentation decisions are often duplicated across multiple pages or transform modules. A global change can force you to change several files.

Two Step View deals with this problem by splitting the transformation into two stages. The first transforms the model data into a logical presentation without any specific formatting; the second converts that logical presentation with the actual formatting needed. This way you can make a global change by altering the second stage, or you can support multiple output looks and feels with one second stage each.

In ASP.NET MVC 4 you can select either Razor View Engine or ASPX View Engine


   For example, a code block in ASPX might look like this:  

<% foreach(var item in Model) { %>
    <tr>
        <td><%: item.Name %></td>
    </tr>
<% } %>

  Whereas the Razor equivalent will look like this:  

@foreach(var item in Model) {
    <tr>
        <td>@item.Name</td>
    </tr>
}

 A view engine is what MVC uses to find and render the views you are requesting from the controller. If you are satisfied with the default routing you probably won’t need to change anything, but let’s say you wanted to have your shared files usually located in root/views/shared to instead be located in root/common, a custom view engine is what you will need to create to be able to do that.

To Build a Custom View Engine: - http://coderjournal.com/2009/05/creating-your-first-mvc-viewengine/

 Different Types of View Engines & Read More

Razor :- http://weblogs.asp.net/scottgu/archive/2010/07/02/introducing-razor.aspx

ASPX :- http://aspnet.codeplex.com/

Spark :- http://sparkviewengine.com/

NHaml :- http://code.google.com/p/nhaml/

NDjango :- http://ndjango.org/index.php?title=NDjango_Home

Hasic :- http://www.codeproject.com/Articles/467850/ASP-NET-MVC-view-engines

Brail :- http://mvccontrib.codeplex.com/wikipage?title=Brail&ProjectName=mvccontrib

Bellevue :- http://www.ope.ag/Bellevue/Page/intro

SharpTiles :- http://www.sharptiles.org/

String Template :-  http://code.google.com/p/string-template-view-engine-mvc/

Wing Beats :-  http://wingbeats.codeplex.com/

SharpDOM :- http://sharpdom.codeplex.com/ 

 

  Short Description On Project Template In ASP.NET  MVC 4  

 

Empty: - The Empty template created the minimum references and resources required to run an Asp.net MVC application. As you can see in below image,  Models, Controllers, App_Data are completely empty folders and View folder only contains web.config and a Global.asax file and web.config. App_Start folder contains 3 configuration files ( FilterConfig.cs, RouteConfige.cs and WebApiConfig.cs ). 

 

 Basic: - It’s a new project type in MVC 4 and it was not available in MVC3. Basic project is much much closer structurally to Empty project in MVC3. It includes Contentand Scripts as well as few more references. Here is a short list of what’s included

jQuery

jQuery UI

jQuery Validation

modernizr

KnockoutJS

Antlr 3

Entity Framework

WebGrease

Bundling and minimization facilities have been prepared in this template. BundleConfig.cs file has been added to App_Start folder. Additionally, what had been disappeared in Views folder came back to the board. _Layout.cshtml includes the jQuery bundle as well as the default theme styling. 

 

 Internet Application :- Adds tow controllers( Account controller and Home controller)  to the Basic template with implemented actions and views.  Membership management functionality which allows you register, login, change password and so on is implemented in Account controller and Home controller gives you Index, About and Contact actions working with their own related views. Its a template used usually to start a normal web project in action.  

 

 

Intranet Application :- In fact, It’s  the Internet Application except for Membership management.  the Account controller and the web.config has been configured  to use Windows as it’s authentication method.

Mobile Application :-Mobile website programming is one of  most important feature in MVC 4 so this template has everything that Internet Application template has, however it  is using  jQuery.mobile instead.  

 

 Web API  Application :-Its another new Template in VS2012 to make it easy to develop RESTful web services and applications to feed a vast variety rage of clients from desktop browsers to tablet applications. It allows you to expose your data and service to the web directly over  Http. This template includes everything from Basic Template expect Account controller and membership functionality. 

 

 

Step by Step Creating ASP.NET MVC 4 Application - [Hello World] 

 1) Open the Visual Studio 2012 

 

 2) Select File-------->New------------------------>Project 

 3) A popup will be open, where you can see the list of all available templates for Windows and Web application. Select the Web application from the left hand side and also select the Template from the right hand side of the popup i.e. “ASP.NET MVC 4 Web Application”. Here you also provide the Proper application name and also select the folder where you save the application. 

 

 4) Select the “Ok” button, then after again a Popup will be open where you have to select the proper template for the application. Here in this case “Empty” Template is selected from the “Project Template”. 

 

 

 

 

 5) The Empty project will be open which is shown below 

 

 5) Now Right click on the “Controller” folder and then select Add------->Controller. A popup will be open where you give the appropriate name with suffix as a controller and click the add button. HelloWorldController class is opened

 

 6) Right click on the “Index” function in the “HelloWorldController” and select the “Add View…” option and click the “Add” button. 

 

7) “Index.cshtml” file is created inside the “HelloWorld” folder which is inside the “Views” folder. 

 

 8) Now Open the “Global.asax.cs” file and change the following code snipet 

 routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "HelloWorld", action = "Index", id = UrlParameter.Optional }
            ); 

 9) Run the application by pressing the F5 key.

  

   References :-

     MSDN 

     ASP.NET Site  

     Various Internet Site

     Shemeer NS(Author)   

     Wikipedia 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here