articles

Home / DeveloperSection / Articles / Working With Controller in ASP.NET MVC 4

Working With Controller in ASP.NET MVC 4

Anonymous User5939 09-Feb-2015

Hi everyone in this article I’m explaining about Controller class, how to add and how to work Controller class.

In my previous post I’ll intruduced about Start with ASP.NET MVC 4

Description:

The Controller Folder contains the controller classes responsible for handling user input and responses. Mvc requires name of all controllers to end with “controller”. The base class of all controllers is the ControllerBase which provides general mvc handling. The Controller class inherits from ControllerBase and is the default implement of a controller.

Let’s begin by creating a controller class. In Solution Explorer right click the controller folder and then click Add then Controller.

Working With Controller in ASP.NET MVC 4

In the Add Controllerdialog box, give name your new controller and select template Empty MVC Controller and then click Add

Working With Controller in ASP.NET MVC 4

 

Solution Explorerthat a new file has been created named “HomeController.cs” .

Working With Controller in ASP.NET MVC 4

 

Now we have some changes in HomeController.cs file with fallowing changes.

 

using System.Web;
using System.Web.Mvc;
 
namespace StudentManagement.Controllers
{
    publicclassHomeController : Controller
    {
        //
        // GET: /Home/
 
        publicstring Index()
        {
            return"Hi i am default action...";
        }
 
        //
        //GET: /Home/Welcome/
        publicstring Welcome()
        {
            return"This is welcome action...";
        }
 
    }
}

 

When run application first time then our default action return a string in html as an example. The Controller is named HomeController and the first method is name Index invoke by default.

Working With Controller in ASP.NET MVC 4

If you want to call different action method then you pass url like this

/Controller/Action/

Working With Controller in ASP.NET MVC 4

ASP.NET MVC invokes different Controller class and different action name depending on the coming url. The default url routing set in RouteConfig.cs file. In my next post I will explain about URL Routing in MVC

 


Updated 07-Sep-2019
I am a content writter !

Leave Comment

Comments

Liked By