Search Your Dot Net Topic

Friday 16 June 2017

Types of Routing

In previous article you come to know routing definition, basic and how routing working internally in web app.

Previous article link :
(Introduction of Routing)




In this article you will come to know about following point:
Types of Routing
Combining Attribute Routing with Traditional Routing
Which routing method is best?


Types of Routing:
Ø Traditional / Convention Routing.
Ø Attribute Routing.


Ø Traditional Routing:
Which routing setting through RouteConfig.cs  that's called  Traditional Routing. This is default mechanism for routing, routing perform  parse incoming URLs and determine the appropriate actions, controllers, and data to use for
that request.

My previous article detailed about on traditional routing.

Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace RoutingMvcWebApp
{
    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 = "<Your Controller Name>", action = "<Your Action Method Name>", id = UrlParameter.Optional }
            );
        }
    }
}

  




Ø Attribute Routing:
Doing the same thing and working pattern is same as traditional routing but with different manner. In attribute routing we are defining the route name on the top / head of controller or action method.

routes.MapMvcAttributeRoutes(); //Enables Attribute Routing


You can use both routing mechanism in web app.



ActionMethod Attribute Example
Example1:

ActionMethod base example of attribute routing:

   [Route("plastic/index")]
        public ActionResult Index()
        {
            return View();
        }





Run Time route effect in browser.




Example2:

Converting ActionMethod name into parameter in attribute routing:
You can see in sample code just below. In URL user can not see the action method name.


  [Route("plastic/{pid:int}")]
  //Route: /plastic/16
  public ActionResult ProductDetail(int pID)
  {
            ViewBag.Message = "Your contact page.";
           return View();
   }






Controller Attribute Example
For controller level attribute routing there is keyword RoutePreFix(). This will prefix on all ActionMethod name.

Sample Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RoutingMvcWebApp.Controllers
{

    [RoutePrefix("plastic")]
    public class HomeController : Controller
    {
        [Route("list")]
        ///Route: /plastic/list
        public ActionResult Index()
        {
            return View();
        }
    }

    
}








Which routing method is best?
As compare to convention/ traditional routing we have to setting in RouteConfig.cs everytime, but in attribute routing we can set on Controller level as well as on ActionMethod. Attribute routing is more flexible.

Both routing are good and best, use as per your requirement of your project.


Combining Attribute Routing with Traditional Routing:
In routeconfig.cs file just add the following line to enable the Traditional routing with attribute routing.

Sample Code:

routes.MapMvcAttributeRoutes(); //Enables Attribute Routing