In ASP.NET MVC, a controller is a class that handles user requests, processes input, interacts with the model, and returns the appropriate view. Creating a controller is a straightforward process that can be accomplished using Visual Studio. Below are the detailed steps to create a controller in an ASP.NET MVC application, along with sample code.

Step 1: Open Your ASP.NET MVC Project

Start by opening your existing ASP.NET MVC project in Visual Studio. If you do not have a project yet, you can create a new one by selecting File > New > Project and choosing the ASP.NET Web Application template, then selecting the MVC option.

Step 2: Add a New Controller

To add a new controller, follow these steps:

  1. In the Solution Explorer, right-click on the Controllers folder.
  2. Select Add > Controller.
  3. In the "Add Controller" dialog, choose the type of controller you want to create. For example, select MVC 5 Controller - Empty for a basic controller.
  4. Enter a name for your controller (e.g., ProductController) and click Add.

Step 3: Implement the Controller

After adding the controller, Visual Studio will create a new class file for you. Below is an example implementation of a simple ProductController that handles product-related actions:

        
using System.Collections.Generic;
using System.Web.Mvc;

public class ProductController : Controller
{
// Action method to display the list of products
public ActionResult Index()
{
var products = new List<Product>
{
new Product { Id = 1, Name = "Product 1", Price = 10.00M },
new Product { Id = 2, Name = "Product 2", Price = 20.00M },
new Product { Id = 3, Name = "Product 3", Price = 30.00M }
};
return View(products);
}

// Action method to display product details
public ActionResult Details(int id)
{
var product = new Product { Id = id, Name = "Product " + id, Price = 10.00M * id };
return View(product);
}
}

In this example:

  • The ProductController class inherits from the Controller base class.
  • The Index action method retrieves a list of products and returns the Index view, passing the product list as the model.
  • The Details action method takes an id parameter, simulates retrieving a product based on that ID, and returns the Details view with the product data.

Step 4: Create Views for the Controller Actions

For each action method in your controller, you should create a corresponding view. To create a view for the Index action:

  1. Right-click on the Views folder, then select Add > New Folder and name it Product.
  2. Right-click on the Product folder, then select Add > View.
  3. Name the view Index and click Add.
        
@model IEnumerable<Product>

<h2>Product List</h2>
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Price</th>
</tr>
</thead>
<tbody>
@foreach (var product in Model)
{
<tr>
<td>@product.Id</td>
<td>@product.Name</td>
<td>@product.Price</td>
</tr>
}
</tbody>
</table>

This view displays a list of products in a table format. The model for the view is an enumerable collection of Product objects, which is passed from the Index action method of the ProductController.

Step 5: Run the Application

After creating the controller and views, you can run your application. Navigate to the URL corresponding to your controller (e.g., /Product/Index) to see the product list displayed on the page.

Conclusion

Creating a controller in ASP.NET MVC involves adding a new class, implementing action methods, and creating corresponding views. This process allows you to handle user requests and display data effectively. By following the steps outlined above, you can create controllers that form the backbone of your ASP.NET MVC applications, enabling a clean separation of concerns and a structured approach to web development.