MVC Routing in Details

Hi All, This is my another blog on MVC.
In this blog, I have explained Routing in MVC. Here I will show how to create a route for different kinds of URL.

What is Routing in ASP.Net MVC?

MVC stands for Model, View, and Controller. But there is one hidden element of MVC which is Route.

MVC route is the most important section in MVC pattern. Routing in MVC matches incoming requests to a controller action.
Routing is the way to map your action of a controller. So, basically, routing defines which action method of which controller will be called for defined URL pattern. ASP.Net MVC supports routing by default. Basically, MVC routes define the URL pattern.

MVC 5 supports Attribute Routing

Let’s demonstrate this by an example.

Whenever you create MVC project in Visual Studio, a default route is added in RouteConfig.cs file which is in App_Start folder.

Default route created in Visual Studio

routes.MapRoute(
                name: “Default”,
                url: “{controller}/{action}/{id}”,
                defaults: new { controller = “MyProject”, action = “Index”, id = UrlParameter.Optional }
            );

id is optional here.
So below is the URL
http://localhost/<controller-name>/<action-method>
Which will call given Action method from given Controller name.

Now, suppose you want to use a different URL structure or want to show .aspx or .html at the end of URL or any other URL pattern as per your choice or requirement.

Look at below route.

  routes.MapRoute(
          name: “MyCustom Route”,
          url: “mylandingpage/productpage.html”,
          defaults: new { controller = “Product”, action = “GetProducts” }
      );

If a user requests this page – http://localhost/mylandingpage/productpage.html then GetProducts as action method from Product Controller will be executed.

Now let’s see below route –

  routes.MapRoute(
          name: “MyCustom Route1”,
          url: “mypage/product/{category}/{productname}”,
          defaults: new { controller = “ProductController”, action = “ProductDetails”, category = UrlParameter.Optional }
      );

Similarly, you can create your own URL pattern and define the action and controller.

Note: name parameter in MVC route should be unique, else you will get this error –

A route named ‘<route name>’ is already in the route collection. Route names must be unique.
Parameter name: name

So, localhost/product means ProductController and then an Action of that controller class.

Let’s see some of the sample examples in MVC for below Route.

routes.MapRoute(
                name: “Default”,
                url: “{controller}/{action}/{id}”,
                defaults: new { controller = “Products”, action = “Index”, id = UrlParameter.Optional }
            );

/products/bestrated – ProductsController and BestRated() as action.

/products/edit/1 – ProductsController and Edit(int id) as Action in that controller.

What if someone has passed the only controller name in the URI i.e. localhost/Home, in this case, default route with given Action will work.
/Products means ProductsController and Index() as action result.

Hope you understand MVC route, please comment if you have any concern or you are happy to read this blog.

Next –

Leave a Comment

RSS
YouTube
YouTube
Instagram