Getting Started With ASP.NET MVC - Part Two

This article explains the basics of ASP.NET MVC and explains routing in MVC, MVC routing table, Action methods, and action selectors. Here, we can learn Getting Started with ASP.NET MVC.

Introduction

This article explains the basics of ASP.NET MVC, such as  - routing in MVC, MVC routing table, action methods, and action selectors. Here, we will learn how to get started with ASP.NET MVC. Read the previous part of the article before continuing with this one.

What is ASP.NET MVC

MVC is an architectural pattern. It separates the application into three main components - Model, View, and Controller. It handles the specific development aspects of an application. MVC is the most frequently used industry-standard web development framework.

Routing in MVC

A route is a URL pattern that enables you to use the URLs by initiating the process of directing an HTTP request. Routing is a part of MVC architecture and is basically the process of mapping incoming browser requests to particular MVC controller actions. There are two impart namespaces - the first one is “System.Web.Mvc” and the other one is “System.Web.Routing”.

We have to mention the default routes in “RoutConfig” class. The default routing contains three properties - name, URL, and defaults.

  • In the name property, we give the routing name. We cannot give the same routing name to another routing.
  • The URL property describes the pattern of URL. The requested URL should match the mentioned pattern otherwise it throws an error.
  • The default property mentions the default Controller and Action name. ID is optional. If needed, we can pass through the URL, otherwise, there is no need to pass the ID value.

Example

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

Route Table

A Route Table stores the URL routes for an application. RouteTable is the class that initializes a new instance of the System.Web.Routing. The RouteTable class uses “RouteTable()” constructor. RouteTable contains “RouteCollection” property with “Routes”. “Routes” is an object of “RouteCollection” that contains all the routes in collection.

Example

  1. using System.Runtime.CompilerServices;  
  2.   
  3. namespace System.Web.Routing  
  4. {  
  5.     public class RouteTable  
  6.     {  
  7.         public RouteTable();  
  8.   
  9.         public static RouteCollection Routes { get; }  
  10.     }  
  11. }  

Register Routes

We are registering routes in “Application_Start()”. Application_Start() is located in Global.asax.cs file in MVC. The Application_Start() method is called while running the application for the very first time.

Register Routes

Routing Pattern

We can give different URL patterns in “RouteConfig”. Using URL properties, we can define a pattern. The pattern defines the request URL. The default URL pattern is “{controller}/{action}/{id}", and we can see the example below.

Example Type 1

  1. routes.MapRoute(  
  2.     name: "Default",  
  3.     url: "{controller}/{action}/{id}",  
  4.     defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }  

The request URL should be like: “http://localhost:56302/Home/About”. The domain name or local host as well as port number, is default. As per the pattern, the first one is controller name, second one is action name, and id is optional.

Example Type 2

  1. public static void RegisterRoutes(RouteCollection routes)  
  2.         {  
  3.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  4.   
  5.             routes.MapRoute(  
  6.                 name: "Default",  
  7.                 url: "{controller}/{action}/{id}",  
  8.                 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }  
  9.             );  
  10.   
  11.             routes.MapRoute(  
  12.                 name: "MyCustomRoute",  
  13.                 url: "Demo/{action}/{id}",  
  14.                 defaults: new { action = "Index", id = UrlParameter.Optional }  
  15.             );  
  16.         }  

Here, we have added “MyCustomRoute” routing with different URL patterns. In that, routing pattern is "Demo/{action}/{id}". This pattern controller name is always “Demo”, however, the action name can change. We can refer to the above example. The same way we can define different patterns.

Action Methods

It is like normal methods. An action method must be public. It cannot be private or protected. The action method cannot be overloaded. The action method cannot be a static method.

Action methods are the return type of “ActionResult”. ActionResult is an abstract class and it is based on different view results. An Action Result represents the result of an action method. It initializes a new instance of the System.Web.Mvc.ActionResult class. We can find the Action Result abstract class below.

  1. namespace System.Web.Mvc  
  2. {  
  3.     public abstract class ActionResult  
  4.     {          
  5.         protected ActionResult();  
  6.   
  7.         public abstract void ExecuteResult(ControllerContext context);  
  8.     }  
  9. }  

Each Controller contains many action methods. Based on the request, the corresponding controller and action method will be invoked. We can find the action methods like in the below code.

  1. public class HomeController : Controller  
  2.     {  
  3.         public ActionResult Index()  
  4.         {  
  5.             return View();  
  6.         }  
  7.   
  8.         public ActionResult About()  
  9.         {  
  10.             ViewBag.Message = "Your application description page.";  
  11.   
  12.             return View();  
  13.         }  
  14.   
  15.         public ActionResult Contact()  
  16.         {  
  17.             ViewBag.Message = "Your contact page.";  
  18.   
  19.             return View();  
  20.         }  
  21.     }  

Each action method contains the View. Action methods return the results to the View page and the View page contains “.CSHTML” extensions.

Register Routes 

An action method returns different types of View results. Kindly refer to the below article to know more about different types of View result.

Conclusion

This article explained about routing in MVC, the routing table in MVC, Action methods, and action selectors. I hope this is very helpful for new learners.