ASP.NET MVC :Add Controllers and Action Methods

Add Controllers and Action Methods in ASP.NET MVC Applications:



ASP.NET MVC Controllers:

  • Controllers Folder contains the controller classes
  • MVC Controller is a simply class file of C# code.
  • A MVC Controller has some Action methods.
  • MVC requires the name of controllers to end with "Controller" word.
  • We create the following files “HomeController.cs” (for the Home page) see the given Image.
ASP.NET MVC :Add Controllers and Action Methods by Parijat


How to Add Controller in MVC Application:

Use the Fallowing steps for Adding a Controller in asp.net MVC web project. These are –

Step first:
Open .net IED and create an Empty MVC Application. If you don’t know then view my first post how to start work with asp.net MVC project.



Step two:
Right click on Controllers folder and add new controller as like that.

ASP.NET MVC :Add Controllers and Action Methods by parijat


Step three:
Give the name of controller and click on ok button.

Asp.net MVC controller’s C# code :

You can see the MVC controller “HomeController.cs” is create and the code is –

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }

    }
}

Action Method in MVC Controller Class:

In this controller class you have a ActionResult Index() Action Method.
This Method returns to control in a view.

if you want to more about MVC then visit these posts:-




For learning What is View and how to add a view in MVC Application see the next post.

If you want to more than you can add here.

Asp.net related Post:

Comments