Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Attribute Routing in ASP.NET MVC 5

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 325
    Comment on it

    Attribute Routing in ASP.NET MVC 5

     

    Matching URI to an action in ASP.NET MVC is routing. ASP.NET MVC supports convention routing which is allowing the developer to specify formats which are then used to parse incoming URLs and thereby identify appropriate controllers, action and data used for incoming request.

     

    Attribute Routing

     

    ASP.NET MVC 5 introduced a new type of routing called attribute routing. Route attribute are used to specify route in attribute routing. Attribute routing are considered to be more flexible then convention routing in developer's point of view because routes are placed next to action that actually uses them. Developer have more control over URIs in web application in attribute routing.

     

    Enabling Attribute Routing:

     

    • In  App_Start folder in ASP.NET MVC open RouteConfig.cs file.
    • Add  routes.MapMvcAttributeRoutes() method for Attribute Routing. Following is the RouteConfig.cs file with routes.MapMvcAttributeRoutes().
    public class RouteConfig
        {
            public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
                routes.MapMvcAttributeRoutes();//For Attribute Routing
    
                routes.MapRoute(               //ForConvention-based Routing 
                    name: "Default",
                    url: "{controller}/{action}/{id}",
                    defaults: new { controller = "Home", action = "Index",
                                    id = UrlParameter.Optional }
                );
            }
        }

     

    Attribute and convention based routing can be placed and implemented together in same file. But attribute routing should configure before convention based routing.

     

    Example of Attribute Routing :-

     

    public class HomeController : Controller
    {
    
        //URL: /MvcRoutetest
        [Route(MvcRoutetest)]
        public ActionResult Index()
            ViewBag.Message = "Welcome to ASP.NET MVC 5 Atrribute Routing!";
            return View();
    }}

     

    In above example a route attribute is defined near to action method.

     

    Example of Attribute Routing with Optional Parameter :-

     

    public class HomeController : Controller
    {
    
      // Optional URI Parameter
      // URL: /MvcRoutetest/
      // URL: /MvcRoutetest/00234567
    
        [Route(MvcRoutetest /{ employeeName ?})]
        public ActionResult RouteTestDescription(string employeeName)
            ViewBag.Message = "Welcome to ASP.NET MVC 5 attribute routing with optional parameter!";
            return View();
    }

     

    In above example an optional parameter is defined by question mark "?". When a Url "/MvcRouteTest" or "MvcRouteTest/00234567" is requested in an application, these URLs will be routed to RouteTestDescription  action.   

     

    Route Prefixes

     

    In Route Prefix for entire controller and it's action method have a common prefix. RoutePrefix keyword is used for this purpose. In this case the common prefix is defined above controller and is prepended to every route attribute used in action within that controller. 

    [RoutePrefix(MvcRoutetest)]
    public class HomeController : Controller
    {
        //URL: /MvcRoutetest/
        [Route]
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC attribute routing!";
            return View();
        }
    
      // Optional URI Parameter
      // URL: /MvcRoutetest/
      // URL: /MvcRoutetest/0023456
        [Route({ employeeName })]
        public ActionResult RouteTest(string employeeName)
        {
            ViewBag.Message = "Welcome to ASP.NET MVC attribute routing!";
            return View();
        }
    }

     

    If an action does not have a route attribute then it does uses attribute routing, for that action convention based routing is used. With attribute routing it is easy to troubleshoot issues and modification of anything does not lead to break down of another route.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: