Attribute routing with asp.net MVC5

Until the latest version of asp.net (asp.net mvc4), routing was handling by Convention-based routing to matches a URI to an Action in a separate class (RouteConfig) registered in global.asax. but in asp.net MVC5  you can use attribute routing. It means you can use attribute to set routes to your actions. It's too easy getting started with attribute routing, you just need to refer RouteConfing.cs in App_Start folder and add  routes.MapMvcAttributeRoutes(); in RegisterRoutes:

  1.  public class RouteConfig
  2.     {
  3.         public static void RegisterRoutes(RouteCollection routes)
  4.         {
  5.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  6.             routes.MapMvcAttributeRoutes();
  7.             routes.MapRoute(
  8.                 name: "Default",
  9.                 url: "{controller}/{action}/{id}",
  10.                 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
  11.             );
  12.         }
  13.     }

 

 

Now you should define attribute for your actions, create a simple Data class and some fake data:

  1.    public class SampleController : Controller
  2.     {
  3.        private List<Data> _datas = new List<Data>()
  4.         {
  5.             new Data {Id = 1, Name = "Ehsan"},
  6.             new Data {Id = 2, Name = "Micheal"},
  7.             new Data {Id = 3, Name = "Elizabeth"},
  8.             new Data {Id = 4, Name = "Sia"},
  9.             new Data {Id = 5, Name = "Behzad"},
  10.             new Data {Id = 6, Name = "Emrah"},
  11.             new Data {Id = 7, Name = "Anahita"}
  12.         };
  13.  
  14.       public ActionResult List()
  15.         {
  16.             var data = _datas;
  17.             return View(data);
  18.         }
  19.  
  20. }
  21.   public class Data
  22.     {
  23.         public int Id { get; set; }
  24.         public string Name { get; set; }
  25.     }
  26.  

Now run the application an navigate to List action of Sample Controller:

 

opps!

 

oops!  Remember that you told MVC to use attribute routing, so you should add the attribute routing to List action:

  1.         [Route("Sample/List/{Id?}")]
  2.         public ActionResult List()
  3.         {
  4.             var data = _datas;
  5.             return View(data);
  6.         }

 

Now it's ok to get a list of data in list view. to completing our sample add this piece of code in List view to be able to call other actions:

  1. @model List<AttributeRouting.Controllers.Data>
  2. @{
  3.     ViewBag.Title = "List";
  4. }
  5.  
  6. @foreach (var item in Model)
  7. {
  8.     @Html.ActionLink(item.Name, "Data", "Sample", new { id = item.Id }, null)
  9.     <br>
  10. }

 

Now add the Data action with Id parameter:

  1.         [Route("Sample/{Id?}")]
  2.         public ActionResult Data(int id)
  3.         {
  4.             var data = _datas.Select(c => c.Id == id);
  5.             return View();
  6.         }

 

If you call this action, Url should be is something like this:   http://localhost:54524/Sample/1

Now add another Data action with a different signature:

  1.        [Route("Sample/{Id}/{name?}")]
  2.         public ActionResult Data(int id, string name)
  3.         {
  4.             return View();
  5.         }

 

As you can see the name is in routing but it's optional, if you add another ActionLink in List view: 

  1. @foreach (var item2 in Model)
  2. {
  3.     @Html.ActionLink(item2.Name, "Data", "Sample", new { id = item2.Id, name = item2.Name }, null)
  4.     <br />
  5. }

 you can render the second Data action, and it would be something like this in browser:  http://localhost:54524/Sample/1/Ehsan

 

Create another action:

  1.         [Route("Sample/BlahBlah/{Id}")]
  2.         public ActionResult Another(int id)
  3.         {
  4.             return View();
  5.         }

 

And the related link in list view:

  1. @foreach (var item3 in Model)
  2. {
  3.     @Html.ActionLink(item3.Name, "Another", "Sample", new { id = item3.Id }, null)
  4.     <br />
  5. }   

 

Now if you navigate to the action, it would be :  http://localhost:54524/Sample/BlahBlah/1

By the way, you don't need to repeat the controller name in every action, you can use  [RoutePrefix("Sample")] for your controller to be available for all actions:

  1.     [RoutePrefix("Sample")]
  2.     public class SampleController : Controller
  3.     {
  4.          …
  5.     }

 

Cheers!


Tags: C# Asp.Net MVC


comments powered by Disqus