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

Step 2: Update the file with the following code:
- using System.Web.Mvc;
- using System.Web.Routing;
- namespace DemoMvc
- {
- public class RouteConfig
- {
- public static void RegisterRoutes(RouteCollection routes)
- {
- routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
- routes.MapRoute(
- name: "Sample",
- url: "{controller}/{action}",
- defaults: new { controller = "Sample", action = "GetStudent" }
- );
- }
- }
- }
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.



Hong TrieuPosted May 6, 2014, 12:27 AM
Cool.