diff --git a/2_Routing.md b/2_Routing.md index 3d21415..55d7f2e 100644 --- a/2_Routing.md +++ b/2_Routing.md @@ -117,3 +117,62 @@ namespace FirstMVCDemo } ``` +## Custom Routing in ASP.NET MVC App + +### Creating Custom Routes in ASP.NET MVC App + +* Using `MapRoute` extension method. +* Need provide at least **route name** & **URL patter**. +* Default parameter is optional +* Multiple custom routes can be created with different names + +```csharp +namespace FirstMVCDemo +{ + public class RouteConfig + { + public static void RegisterRoutes(RouteCollection routes) + { + routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); + + routes.MapRoute( + name: "Employee", + url: " Employee/{id}", + defaults: new + { + controller = "Employee", + action = "Index" + } + ); + + routes.MapRoute( + name: "Default", //Route Name + url: "{controller}/{action}/{id}", //Route Pattern + defaults: new + { + controller = "Home", //Controller Name + action = "Index", //Action method Name + id = UrlParameter.Optional //Defaut value for above defined parameter + } + ); + } + } +} +``` + +Following URLs will be mapped to Employee route: +* `http://localhost:xxxxx/Employee` +* `http://localhost:xxxxx/Employee/Index` +* `http://localhost:xxxxx/Employee/Index/3` + +Rules of creating route: +* Always put more specific route on top + +## Route Constraints in ASP.NET MVC + + +## Attribute Routing in ASP.NET MVC + +## Attribute Routing with Optional Parameter + +## Route Prefix in Attribute Routing \ No newline at end of file