Click here to Skip to main content
15,885,216 members
Articles
(untagged)

Basic Understanding On ASP.NET MVC 4

Rate me:
Please Sign up or sign in to vote.
4.82/5 (24 votes)
4 May 2013CPOL7 min read 174.9K   31   7
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. 

Image 1 

Image 2

Release History of ASP.NET MVC 

Image 3

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.

 Image 4

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

Image 5

   For example, a code block in ASPX might look like this:<o:p>  

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

  Whereas the Razor equivalent will look like this:<o:p>  

@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  

Image 6 

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 ). 

Image 7

 

 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. 

 Image 8

 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.  

 Image 9

 

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.  

Image 10 

 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. 

 Image 11

 

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

 1) Open the Visual Studio 2012 

Image 12 

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

Image 13

 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. 

 Image 14

 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”. 

 Image 15

 

 Image 16

 

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

Image 17 

 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

Image 18

 

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

 Image 19

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

 Image 20

 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.

<o:p>

 Image 21 

   References :-

     MSDN 

     ASP.NET Site  

     Various Internet Site

     Shemeer NS(Author)   

     Wikipedia 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhelloworld issue Pin
erummirza31-Aug-14 22:02
erummirza31-Aug-14 22:02 
I m not getting Helloworld text on screen ..it shows index
Questionhelloworld issue Pin
erummirza31-Aug-14 22:01
erummirza31-Aug-14 22:01 
QuestionStep 8 Pin
eng.mohamed.said8015-Sep-13 21:49
eng.mohamed.said8015-Sep-13 21:49 
SuggestionRe: Step 8 Pin
Afazal MD 310420911-Nov-13 0:26
professionalAfazal MD 310420911-Nov-13 0:26 
QuestionWorks until #8 Pin
StealthMicro5-May-13 13:13
professionalStealthMicro5-May-13 13:13 
AnswerRe: Works until #8 Pin
Santhosh Nalumachu 29-Aug-13 0:17
Santhosh Nalumachu 29-Aug-13 0:17 
Questionabout ur article Basic Understanding On ASP.NET MVC 4 Pin
Nilesh C4-May-13 3:04
Nilesh C4-May-13 3:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.