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.

Day 1 :- Displaying Simple HelloWorld using MVC


Introduction


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.



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.


Image1

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.


Image2

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.


Image3

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


Image4

Step 5 :-

Once you click ok the visual studio project gets created as shown in the below figure. Let us first add a controller.


Image5

So right click on the controllers folder -> Add -> Controller menu as shown in the below figure.


Image6

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



Image7

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.


Image8

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


Image9

So right click on that folder and click -> Add -> View.


Image10

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


Image11

In the view put the below code.


Image15

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

Below is a very common error you will get when you run your MVC application for the first time.

Image13

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.

Image14


Comments

Author: Gaurav Aroraa13 Nov 2014 Member Level: Gold   Points : 0

Awesome! Article. I am looking forward for next articles

Author: Abhijit Patil13 Nov 2014 Member Level: Gold   Points : 0

Cheers !!!!

Now MVC series on DNS by shiv sir..

Awesome Artical ,Waiting for all the series.



regards,

Abhijit Patil
dotnetbyabhipatil@gmail.com

Author: Sandeep Reddy B17 Nov 2014 Member Level: Bronze   Points : 0

Really its very helpful :)

Author: Sridhar Thota26 Nov 2014 Member Level: Gold   Points : 0

thanks shiv sir.

waiting for next article



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: