2
Reply

Explain Attribute Routing in ASP.NET MVC5?

Suresh Kumar

Suresh Kumar

7y
958
1
Reply

    1. Reduces the chances for errors, if a route is modified incorrectly in RouteConfig.cs then it may affect the entire application's routing.2. decouple controller and action names from route entirely.3. Easy to map two routes pointing to the same action.Attribute Routing is a more flexible solution than Convention Routing, if only because it allows you quite a bit more flexibility and places the routes next to the actions that will actually use them. However there are certainly benefits to using both in tandem, particularly in situations when you know how some routes will look but aren't sure about others.

    we use friendly URLs that are mapped to controller’s actions instead of physical files as in case of ASP.NET WebForms. Now in ASP.NET MVC5, we can use attributes to define routes giving better control over the URIs. We can easily define routes with Controller’s action methods as follows:public class HomeController:controller { [Route("Product/{productId?}")] public ActionResult Product(string prodcutId) { return view() }}