Learn ASP.NET MVC 5 Step by Step in 30 days– Day 1
ASP.NET MVC is a very wanted skill in todays programming world.In this article we will learn MVC step by step in 30 days. In day 1 we look in to from where to install visual studio , how to create a simple first MVC application and also we will look in to issues which arises when we publish our first MVC program.
This will be a series of article which aims at teaching you MVC step by step in 30 days. So let's start with the first Lab displaying a simple HelloWorld program using MVC. This lab has 7 steps so let's go step by step and in case you miss any of the step please see the below MVC video to catch up.Day 1 :- Displaying Simple HelloWorld using MVC
Introduction
Step 1:-
Download Visual studio ultimate edition from
http://www.microsoft.com/en-in/download/details.aspx?id=43727
Step 2 :-
Once you have installed visual studio click on start – program files and open visual studio ultimate edition.Click on "Visual C#" menu, click on Web and select "ASP.NET Web Application" template as shown in the below figure.
In case you are wondering where all the other templates of ASP.NET have gone just , do not panic. In VS 2013 Microsoft introduced the concept of ASP.NET one. If you see logically "Web Form" , "MVC" and "Web API" they all use ASP.NET framework internally. So why we need to have different templates for the same.
In case you ever want to switch to the old way you can click on "Visual studio 2012" menu where you would find those separate templates.
Step 4 :-
Once you go inside visual studio you need to make a choice of the technology on which you want to code. For now we will select MVC.Now because this is our first MVC hello world program we will not get involved in to complexities of unit tests and security.So Click on Change Authentication and set it to "No Authentication".
Step 5 :-
Once you click ok the visual studio project gets created as shown in the below figure. Let us first add a controller.So right click on the controllers folder -> Add -> Controller menu as shown in the below figure.
Select "MVC 5 Controller – Empty" Scaffold templates as shown in the below figure. Scaffolds are nothing but templates. Because we are just displaying our first hello world program we will use empty template to keep it simple.
In case you want to see how scaffolding template works see this video
Once you have clicked on the empty scaffold give a controller name. But please do not delete the word controller. Controller is a reserved keyword.
In this controller we have created an action called as "SayHello" and this action returns a view "HelloView". Below is the code for the action "SayHello".
public class FirstMVCController : Controller
{
//
// GET: /FirstMVC/
publicActionResultSayHello()
{
return View();
}
}
Step 6 :-
Once you have coded the controller the next step is to add "HelloView" view. Now if you see your project folders you will see he has already created a folder with the name "FirstMVC".So right click on that folder and click -> Add -> View.
Give a nice view name as shown in the below figure. Because this is our first MVC application let not select any model and uncheck the check box "Create as a partial view".
In the view put the below code.
Step7 :-
Press control + F5 to run the program.Now in order to invoke the controller and action we need to type the URL in a proper format. The url should be typed with server name followed by the controller name minus the word "Controller" and then action name.So in our case it will be
http://localhost/FirstMVC/SayHello. If you type the URL format properly and press enter you should see your MVC application running.
Below is a very common error you will get when you run your MVC application for the first time.
Let's try to understand what the aboveerror says. The above error says that the view i.e. "HelloView" view should be a part of shared folder or the controller folder. For example in our situation the controller name is "FirstMVC" so either you shift your view in to "FirstMVC" folder or you move it to the "shared" folder as shown in the below figure.
Awesome! Article. I am looking forward for next articles