Day #1: Learn ASP.NET MVC 5- Adding Controller

 

After long time, I am back to web development. At current web development paradigm, ASP.NET MVC framework is one of the most popular and heavily used web development framework. Seeing popularity and usefulness of ASP.NET MVC, I have decided to write easy to go blog series on this. In this blog post series, each day I will share my learning in form of blog posts.

In day 1, let us learn about Controller. Very first in simpler words,

“Controller does all the actions in MVC. It handles user interactions or inputs. It passes users’ data to Model and selects a View to render on UI”

image

In more coding terms, Controller can be define as below,

  1. Controller is a class
  2. It contains one or more methods called Actions
  3. An Action can return simple string or a selected View to UI
  4. Action takes all browser requests or user inputs
  5. It retrieves data from Model
  6. It selects View to be rendered on UI

You can add a controller by right clicking on Controllers folder in solution explorer. In context menu select Add->Controller. There are different types of Controller templates are available to add. Let us go ahead and select MVC 5 Controller- Empty template to add controller.

image

Next give name to Controller. I am giving name as ProductController.

image

In solution explorer, you will notice In Controller folder, ProductController.cs file being added and in Views folder, there is subfolder Product being added.

image

Next let us go ahead and remove default code created and following two actions. First Action is Index (It is default) action. Index action does not take any input parameter. Second Action added is Welcome. It takes two input parameters id and name.

 


public class ProductController : Controller
{
//
// GET: /Product/
public string Index()
{
return "this is default action";
}

public string Welcome(string name, int id)
{
return "this is welcome action" + name + "with id " + id;
}
}

Following points are worth noticing about ProductController.

  1. ProductController class extends Controller class
  2. Actions are created as methods.
  3. In this case Actions are returning string.
  4. Actions can return other types as well. We will discuss them in further posts.
  5. Actions can take zero or more input parameter.

Let us go ahead and run application. Default action of Product controller is index. On navigating Product Controller, Index action will be called. In this case Index action is returning a string.

clip_image002

We have put one more action Welcome in Product controller. This takes two input parameters Id and name. You can call Welcome action as below. We are passing two input parameters as query string.

image

Better approach to pass input parameters are as Route Data than query string. Id is default input parameter. So instead of passing id as query string, you can pass it as route data as well.

clip_image002[6]

You can find default route and parameters in RouteConfig.cs.

 

clip_image003

 

In above code as you see default controller is Home, default action is Index and default optional parameter is id. If you want to pass other input parameters as route option, you can very much do that by adding more routes.

clip_image005

We have added a route with name and id as parameter. On running application, now you can pass name and id as route option.

clip_image007

This is the basic of working with Controllers in MVC. In further posts we will get into deeper concepts of Controllers. I hope you find this post useful. Thanks for reading.

About these ads

3 thoughts on “Day #1: Learn ASP.NET MVC 5- Adding Controller

  1. Pingback: Dew Drop – May 12, 2014 (#1774) | Morning Dew

  2. Pingback: Day #2: Learn ASP.NET MVC 5- Adding View | debug mode......

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