Creating Your First MVC 4 Application

In this tutorial i will show the example on MVC 4.

Follow this step to create new MVC Application on Microsoft Visual Studio 2010.

Creating MVC 4 Project

  1. File >> New Project
  2. Choose ASP.NET MVC4 Web Application. (If you dont have MVC 4, Install it here)
  3. On Project Template, choose Empty if you want to start from zero or choose Internet Application to start developement with default template.
  4. Choose View engine Razor.
  5. Click OK button
  6. Now you have create one project with MVC4 Solution

 Adding First Controller

  1. Right click on the Controller folder under Solution Explorer Menu.
  2. Choose Add >> Controller
  3. Rename Controller Name to HomeController
  4. Since this is empty project, so under Scaffolding Options Template choose Empty MVC Controller
  5. Change The default ActionResult Index() like this
  6.  
            public string Index()
            {
                return "Hello World, This is my first MVC Application";
            }

  7. Run the project, You should see the output on the browser like image below.
  8.  

Understanding Route

As well as models, views, and controllers, MVC applications use the ASP.NET routing system, which
decides how URLs map to particular controllers and actions. When Visual Studio creates the MVC project, it adds some default routes to get us started. You can request any of the following URLs, and they will be directed to the Index action on the HomeController:

  • /
  • /Home
  • /Home.Index

So, when a browser requests http://yoursite/ or http://yoursite/Home, it gets back the output from
HomeController’s Index method. You can try this yourself by changing the URL in the browser. At the moment, it will be http://localhost:23859/, except that the port part may be different. If you append /Home or /Home/Index to the URL and hit return, you will see the same Hello World result from the MVC application.

This is a good example of benefiting from following MVC conventions. In this case, the convention is that we will have a controller called HomeController and that it will be the starting point for our MVC application. The default routes that Visual Studio creates for a new project assume that we will follow this convention. And since we did follow the convention, we got support for the URLs in the preceding list.

If we had not followed the convention, we would need to modify the routes to point to whatever
controller we had created instead. For this simple example, the default configuration is all we need.

Note : You can see and edit your routing configuration by opening the Global.asax.cs file

By
NOTE : – If You have Found this post Helpful, I will appreciate if you can Share it on Facebook, Twitter and Other Social Media Sites. Thanks =)

Popular posts from this blog

Example to disable save as certain file type in SSRS Report Viewer

Phone book application example - Java

Modal Popup Message Box ASP.NEt C# Example