Controllers in MVC : Define Custom Route

Introduction

This articles helps you to create Custom Routes in your ASP.NET MVC application. You are familiar with the Overview of Controller from my previous article, here I will describe how to create the custom route and present your controller's action at the start of the MVC application.

In this article, we'll follow up the following sections:

  • Define Custom Route
  • Define New Action in Controller
  • Run the Controller

Define Custom Route

In this section, you'll create the Custom Route in the ASP.NET MVC application. Use the procedure below:

Step 1: Open the RouteConfig.cs file

Route Config in MVC 5

Step 2: Update the file with the following code:

  1. using System.Web.Mvc;  
  2. using System.Web.Routing;  
  3. namespace DemoMvc  
  4. {  
  5.     public class RouteConfig  
  6.     {  
  7.         public static void RegisterRoutes(RouteCollection routes)  
  8.         {  
  9.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  10.             routes.MapRoute(  
  11.                 name: "Sample",  
  12.                 url: "{controller}/{action}",  
  13.                 defaults: new { controller = "Sample", action = "GetStudent" }  
  14.             );  
  15.         }  
  16.     }  
  17. } 

In the code above, you can see that the new MapRoute is defined for the default route. It is the custom route information and I deleted the previous route information. In this route the controller name is Sample and the action name is GetStudent. The controller is defined but this action is not defined yet. So, in the next section we'll define this action.

Define New Action in Controller

Now in this section, we'll create the new action in the SampleController, so just proceed ahead with the following procedure.

Step 1: Suppose we have an Entity Data Model in the application named StudentModel. You can also create this ADO.NET Entity Data Model.

Entity Data Model in MVC

Step 2: Now in the SampleController create a new action method with the following code:

  1. public ActionResult GetStudent()  
  2. {  
  3.     MyDatabaseEntities db = new MyDatabaseEntities();  
  4.     return View(db.Students.ToList());  
  5. } 

Now our entire controller will look like this:

  1. using DemoMvc.Models;  
  2. using System.Linq;  
  3. using System.Web.Mvc;  
  4. namespace DemoMvc.Controllers  
  5. {  
  6.     public class SampleController : Controller  
  7.     {  
  8.         // GET: Sample  
  9.         public ActionResult Index()  
  10.         {  
  11.             return View();  
  12.         }  
  13.         public ActionResult Welcome()  
  14.         {  
  15.             return Content("Hello and Welcome to MVC");  
  16.         }  
  17.         public ActionResult GetStudent()  
  18.         {  
  19.             MyDatabaseEntities db = new MyDatabaseEntities();  
  20.             return View(db.Students.ToList());  
  21.         }  
  22.     }  
  23. } 

That's it. We've created the new ActionResult action named GetStudent for showing the list of Students. Now proceed to the next section.

Step 3: Right-click on GetStudent() and select the Add View option as in the following:

Add View in MVC 5

Run the Application

Now our Controller and view are created. Just run the application.

Home Page in MVC

You can see that the new GetStudent() action displays the result as you run the application because you have set this custom route of your application. Now it works as a home page of your MVC application.

Summary

This article described how to define the custom route in the MVC 5 application. You can also learn how to create the controller and view in this application. Thanks for reading.


Similar Articles