MVC 4 Hello world
In this tutorial, I will show you to create simple MVC 4 Hello world.
Follow the below step to create MVC project
- Create MVC Project.
- Choose internet application.
- after click ok you will see a set of folder created, but just leave it as it is.
- Click run to execute (F5) and you will see nice page without need you to code single line. Done!!
How to display “Hello world”
- Right click “Controllers” folder and choose “Add> Controllers”, and in the controller name key in “HelloWorldContoller”, and leave the rest as default.
- Go to HelloWorldController.cs, paste the following code
public class HelloWorldController : Controller
// GET: /HelloWorld/
public ActionResult Index()
ViewBag.Message = "Hello World!!!";
and then right click on the function “Index”, and choose add View.
- In the View name, key in “Helloworld” and leave the rest as default. Click OK.
- You will notice “HelloWorld” folder has been created under Views folder. then go to Index.chtml in this folder and type these code
ViewBag.Title = "Index";
- Click ‘F5’, and you’re done!
The main idea of this tutorial is to let you understand how the controller and view communicate with each other. When you browse the URL, in this case, is http://65397/helloworld, it will first hit the “Index” in the controller and in the controller save data, “Hello world”, into @viewBag.Message, then only will direct you to View to display the “Hello World”. Hope you enjoy!