Home > DeveloperSection > Forums > How to create a attribute based routing in MVC?
Anupam Mishra

Total Post:135

Points:949
Posted on    February-04-2016 10:18 PM

 ASP.Net C#  ASP.Net  ASP.NET MVC 
Ratings:


 1 Reply(s)
 339  View(s)
Rate this:
Hi Everyone,
I want to know how to create a  attribute based routing in MVC? Please give me a solution.

Thank you.


Anupam Mishra

Total Post:135

Points:949
Posted on    February-04-2016 10:48 PM

Attribute Routing is introduced in MVC 5 that is the ability to add routes to the Route Table via attributes so that the route definitions are in close proximity to their corresponding actions. We will still populate the Route Table, but we will do it in a different manner.
public class HomeController : Controller  
{
    [Route("Users/Index")] //Route: /Users/Index
    public ActionResult Index() { ... }
}
 [Route] attribute  specify a route to be added to the Route Table which maps to this action. The parameters to [Route]'s constructor are where the real functionality happens.
The route attribute says that the "GotoStatement" can be invoked using the URL structure "Users/about".
public class HomeController : Controller
{
       [Route("Users/Statement")]
       public ActionResult GotoStatement()
       {
           return View();
       }
}

Don't want to miss updates? Please click the below button!

Follow MindStick