How to create a attribute based routing in MVC?

Total Post:135

Points:949
 843  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. 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();
           }
    }

      Modified On Apr-11-2018 01:21:39 AM

Answer