HOW TO CREATE A ATTRIBUTE BASED ROUTING IN MVC?

Anupam Mishra

Total Post:135

Points:949
Posted by  Anupam Mishra
 594  View(s)
Ratings:
Rate this:
Hi Everyone,
I want to know how to create a  attribute based routing in MVC? Please give me a solution.

Thank you.
  1. Anupam Mishra

    Post:135

    Points:949
    Re: How to create a attribute based routing in MVC?

    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();
           }
    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!