1.

What is attribute routing

Answer»

To make the development easy and simple as compared to asp.net web form application we use attribute ROUTING, it is one of the different METHOD which works differently routing

We used to define the routing in either Global.asax and RouteConfig.cs where this will decide which URL has to be chosen for the development

We need to put routes.MapMvcAttributeRoutes() in the routeconfig.cs which  enable attribute based routing on our MVC web application that can be done by only one line

Eg.

public static void RegisterRoutes(RouteCollection routes)

{ routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapMvcAttributeRoutes(); routes.MapRoute(   name: "Default",   url: "{controller}/{action}/{id}", defaults: new { controller = "PRODUCT", action = "List", id =      UrlParameter.Optional } ); } In an action method, it is used like this Eg. [ROUTE("products/{id?}")] public ACTIONRESULT Details(string id)    { if (string.IsNullOrEmpty(id)) { return View("List", GetProductList()); } return View("Details", GetProductDetails()); }

Route Prefixes

We put the Route Prefix inside the controller which the request follow the route which is defined under the controller in the action method

Eg.

[RoutePrefix("products")] public class ProductController : Controller { [Route] public ActionResult List() { return View(); } [Route("{id?}")] public ActionResult Details(string id) { if (string.IsNullOrEmpty(id))   { return View("List");   } return View("Details");   } }


Discussion

No Comment Found