Home > ASP.NET MVC > ASP.NET # MVC # 3 – ASP.NET MVC Controllers in Detail:-

ASP.NET # MVC # 3 – ASP.NET MVC Controllers in Detail:-

Controllers:-

Controllers are very important part of MVC Architecture,they are responsible for the communication between Model and Views.

Controllers are concerned with the flow of an application,they take the input from the user and returns the appropriate View to the user via UI.

For a class to be a controller it must at minimum implement the IController Interface.

Naming convention of a controller :- The class name must end with suffix Controller.

The standard approach to writing a Controller is to have it inherit from the System.Web.Mvc Controller abstract base class, which implements the ControllerBase base class. The Controller class is intended to serve as the base class for all Controllers

ActionMethods:-

All public methods of a class that derive from Controller become action methods, which are callable via an HTTP request We can return the appropriate view which we want to display as a return type of this method.

For example suppose  you want to call an action/method on page which will display the o/p as “My MVC Application” on a page,you will write code as

Take one sample controller as follows:-

public class SampleController : Controller
{
    public void OnButtonClick()
    {
    Response.Write(“My MVC Application”);
    }
}

To call this method you will type url as Sample/OnButtonClick

It will give you output as pleas see the URL and putput

My Mvc Application

In previous example we call the method with no parameters.Following example will illustrate how to call an action method with parameter

   public void HelloFriends(string Name)  

{             Response.Write(“Hello Friends My name is – “+Name);         }

To call this method you will type url as Sample/HelloFriends/Ganesh

  You will have output as follows

ParametrizeActionMEthod

In the next blog we will go through the action methods with multiple parameters

Thank You.

Advertisements
Categories: ASP.NET MVC
  1. No comments yet.
  1. June 28, 2011 at 2:38 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: