Page views:
5615

This Controller Layer is the responsible for controlling the application flow and interaction between the Model and the View. Controllers are the brains of an ASP.NET MVC application, requesting data from the Model layer and choosing the correct method of getting that data to the user. Note that the Controller does not perform any of this work directly, its real job is to delegate as much as possible to the model and view layers so that they can do the real work. This article will show you how to create a Controller in ASP.NET MVC 4 application.

 Create New Controller

To create a Controller class, In Solution Explorer right click the Controller folder then select Add > Controller.

Name your New Controller

    Here one thing you should remember that all Controllers must be named by using “Controller” suffix. In this example I’m going name our new Controller as “HomeController”. Leave the default template as Empty MVC Controller

 

Notice the Controller folder in Solution Explorer, A new HomeController.cs class has been created.

 

This Controller class contains action methods that will render View pages. When we have created new Controller class, it has default action method and it returns View. In most cases a Controller action method returns a ViewResult.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

namespace MVC4_DEMO.Controllers

{

    public class HomeController : Controller

    {

        //

        // GET: /Home/

         public ActionResult Index()

        {

            return View();

        }

    }

}

 

ASP.NET MVC invokes different Controller classes and its action methods depend on the incoming URL. 

Pattern: [Controller]/[ActionName]/[Parameters]

Example: http://localhost/Home/Welcome/

The first part of the URl invokes a Controller class, The second part of the URl invokes the action method in the Controller class.


Here I’m going to create some action method and I’ll invoke these methods from browser. First I’ll replace default Index() action method with return type as string. So it’ll return a string of HTML. Following example shows that we have created three action methods with different return values.

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

 

namespace MVC_DEMO.Controllers

{

    public class HomeController : Controller

    {

        //

        // GET: /Home/

         public string Index()

        {

            return "This is the default action";

        }

 

       //

        // GET: /Home/Welcome/

         public string Welcome()

        {

            return "This is the Welcome action method";

        }

 

        //
       
// GET: /Home/Welcome/parameter

 

        public string User(string id)
       
{

           
return "Id=" + id;
       
}

    }

}

First let’s run the application on browser. By default it invokes index() function on HomeController. So, the question is, where is this mapping defined. The mapping is defined in Global.asax. Notice that in Global.asax we have RegisterRoutes() method.


RouteConfig.RegisterRoutes(RouteTable.Routes);

Right click on this method, and select "Go to Definition". Notice the implementation of RegisterRoutes() method in RouteConfig class.

Let’s invoke Welcome() action method that we have defined in HomeController class. Now it return value which we have assigned in that action method.

Then let’s invoke third action method User() with parameter. Now pass id in the URL as shown below and press enter.

I hope you understand the functionality of the Controller. Please comment below if is there any confusion or anything I missed to explain in this article.

Thanks for reading..!! :)


If you enjoyed this article, get email updates (it's free).


Related Articles
  • Creating ASP.NET MVC 4 web application with Empty project templateThis article will show you how to start building ASP.NET MVC 4 web application with Empty project template. Empty project template is basic ASP.NET MVC website generating no code and providing you with nothing to start with. Adding a Model, View & Controller with Empty project template helps you to understand quite clearly than starts with Internet Application template.
  • Introduction to ASP.NET MVCASP.NET MVC provides an alternative approach to traditional ASP.NET web development often referred to as web forms by applying an architectural approach. The MVC or Model-View-Controller pattern splits an application into three separate and distinct layers, the Model layer, the View layer and the Controller layer, each with a very specific set of responsibilities
  • Dynamic Accordion menu or Vertical menu using jQuery in ASP.NET MVCNavigation menu is most important in any web applications. While developing web applications, I used to create stuff more interactive by using jQuery and CSS. It’s quite different to create dynamic navigation menu in ASP.NET MVC. Whether it’s a horizontal or vertical menu, the idea remains same. This article will show you how to create jQuery Accordion menu dynamically in ASP.NET MVC web applicati...
  • Adding a Controller in ASP.NET MVC 4This article will show you how to create a Controller in ASP.NET MVC 4 application. This Controller Layer is the responsible for controlling the application flow and interaction between the Model and the View. Controllers are the brains of an ASP.NET MVC application, requesting data from the Model layer and choosing the correct method of getting that data to the user.
  • Getting started with ASP.NET MVC 5 and AngularJS AngularJS is a powerful javascript framework for dynamic web applications. It is fully extensible for works well with other libraries. The resulting environment is expressive, readable and easy to develop.
  • CRUD operation in ASP.NET MVC using Web API and AngularJS AngularJS is a powerful Javascript framework for dynamic web applications. We often use AngularJS to develop single page application. Here we will explore how to create CRUD (Create Read Update Delete) operation in ASP.NET MVC using Web API and AngularJS.
  • Getting started with ASP.NET MVC 4 I hope you are getting excited to start building ASP.NET MVC 4 application. Visual Studio 2012 IDE is used to build MVC 4 application, also you can install MVC 4 components for Visual Studio 2010. This article will show you that how to start building your first ASP.NET MVC 4 application with internet application template.

Comments
comments powered by Disqus

Thanks for downloading..!!

×
Subscribe via Email
    
message to display in fancybox