ASP.NET MVC: Getting Started (Part 2)
Routing in MVC
The route is a URL pattern. It enables you to use URLs and it is the process of directing an HTTP request. Routing is a part of MVC architecture. It is mapping the incoming browser requests to particular MVC controller actions. There are two impart namespace we are using one is “System.Web.Mvc” and another one is “System.Web.Routing”
We have to mention the default routes in “RoutConfig” class. The default routing contains three prosperities are name, URL, and defaults.
- The name properties we give the routing name, we do not give sane routing name to another routing.
- The URL properties describe the pattern of URL. Requested URL should match the mentioned pattern otherwise getting an error.
- The defaults mention the default Controller, Action name. ID is optional if need we can pass through URL, otherwise no need to pass the ID value.
Example
using System.Web.Mvc;
using System.Web.Routing;
namespace FirstMVcApp
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
Route Table
Route Table stores the URL routes for an application. RoutTable is class. Initializes a new instance of the System.Web.Routing.RouteTable class using “RouteTable()”constructor. Route Table contains “RouteCollection” properties “Routes”. ���Routes” is an object of “RouteCollection” that contains all the routes in the collection.
Example
using System.Runtime.CompilerServices;
namespace System.Web.Routing
{
public class RouteTable
{
public RouteTable();
public static RouteCollection Routes { get; }
}
}
Register Routes
We are registering routes in “Application_Start()”. Application start is in Global.asax.cs file in MVC. Application Start method will call while running the very first time of application.
Routing Pattern
We can give a different URL pattern in “RouteConfig”. Using URL properties can define a pattern. The pattern defines the request URL. Default URL pattern is “{controller}/{action}/{id}", we can see the example below.
Example Type 1
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
We follow the request URL should be like “http://localhost:56302/Home/About”. The domain name or localhost as well as port number, it is the default, as per pattern first one is controller name, the second one is action name and id is optional.
Example Type 2
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "MyCustomRoute",
url: "Demo/{action}/{id}",
defaults: new { action = "Index", id = UrlParameter.Optional }
);
}
Here we have added “MyCustomRoute” routing with different url pattern. In that routing pattern is "Demo/{action}/{id}". This pattern controller name always “Demo” and action name might be changed. We can refer to the above example. The same way we can define a different pattern.
Action Methods
It is like normal methods. The 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 base of different view results. Action Result represents the result of an action method. Initializes a new instance of the System.Web.Mvc.ActionResult class. We can find the Action Result abstract class below.
namespace System.Web.Mvc
{
public abstract class ActionResult
{
protected ActionResult();
public abstract void ExecuteResult(ControllerContext context);
}
}
Each controller contains many action methods. Based on the request corresponding controller and action method will be invoked. We can find the action methods looks like below code.
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
Each action methods contain the view. Action methods return the results to view page. View page contains “.CSHTML” extensions.
Action method returns the different type of view results. Kindly refer the below article link to know about a different type 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. Next article explains about getting started ASP.NET MVC part 3.