Wednesday, June 28, 2017

Few Interesting facts of Attribute routing

We of us know that Attribute Routing has introduced to enhance flexibility of routing in ASP.NET MVC. So, here is my ItemController which marked as “Item” using routeprefix attribute and GetItem(int Id) action is decorated using Route attribute. So, to invoke 
GetItem, the route is “/Item/1” 

    [RoutePrefix("Item")]
    public class ItemController : Controller
    {

        [Route("{id}")]
        public void GetItem(int Id)
        {

        }
    }

Now, let’s think some situation where we want to override behavior of RoutePrefix. We have defined GetItemInfo() action with Route attribute prefixed by “~/” which implies that we need to specify “Item” to invoke the action. So, the route to access GetItemInfo() is
      
    [RoutePrefix("Item")]
    public class ItemController : Controller
    {

        [Route("~/SpecialItem/{id}")]
        public void GetItemInfo(int Id)
        {

        }
    }

Set Global Default Route

We know that, in time of route initialization in config file we can specify default route. Here is another way to implement same. Just specify “~/” in any action to mark the action as default action.

So Route to access GetItemInfo() is : /

    [RoutePrefix("Item")]
    public class ItemController : Controller
    {
        [Route("~/")]
        public void GetItemInfo()
        {

        }
    }


Now, What if we mark more than one action as default action? You will experience following exception.


Set Default route to specific RoutePrefix

If we place Empty string(“”) to some action, it will mark as default route for that RoutePrefix. So, In following example GetItemInfo() is default route under “Item” RoutePrefix.

So, to Invoke GetItemInfo() the route is “/Item”

    [RoutePrefix("Item")]
    public class ItemController : Controller
    {
        [Route("")]
        public void GetItemInfo()
        {

        }
    }

Here is another way to set default route. 

  RoutePrefix("Item")]
  [Route("{action=GetItemInfo}")]
    public class ItemController : Controller
    {
        public void GetItemInfo()
        {
        }
    }

Route matching by pattern

Using attribute routing, we can pick route by pattern. Now, all routes ended with “info” suffix will map to GetItemInfo() action. So, one sample route could be “/Item/anyinfo”

[RoutePrefix("Item")]
   
    public class ItemController : Controller
    {
        [Route("{*info}")]
        public void GetItemInfo()
        {

        }
    }

Set priority to route

We know that if there are multiple action mapping to same route, we will get exception. To get rid of from this situation, we can specify “Order”. In following example we specified Order in “Route” attribute to action. The route “Item/get/id” is mapping to both GetItemInfo() and GetAnotherItemInfo().

public class ItemController : Controller
    {
        [Route("get/{id}", Order =0)]
        public void GetItemInfo(int Id)
        {

        }

        [Route("get/{id}", Order = 1)]
        public void GetAnotherItemInfo(int Id)
        {

        }

    }

If we do not specify “Order” it will throw this exception.


MVC, register low order route value at first, we Once we go with “item/get/1” it will hit GetItemInfo() action.




1 comment:

  1. Attributes hold significant importance in programming. In languages like C#, they provide metadata about elements in the code. Does Streaming Improve It help in creating more flexible and readable programs.

    ReplyDelete