Home > DeveloperSection > Beginner > Working With Controller in ASP.NET MVC 4

Working With Controller in ASP.NET MVC 4


ASP.NET MVC Mvc4  Controller 
Ratings:
0 Comment(s)
 1508  View(s)
Rate this:

Working With Controller in ASP.NET MVC 4

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.

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

 

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

 

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

 

using System.Web;

using System.Web.Mvc;

 

namespace StudentManagement.Controllers

{

    public class HomeController : Controller

    {

        //

        // GET: /Home/

 

        public string Index()

        {

            return "Hi i am default action...";

        }

 

        //

        //GET: /Home/Welcome/

        public string 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.

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

/Controller/Action/

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

 


Don't want to miss updates? Please click the below button!

Follow MindStick