SlideShare a Scribd company logo
Emad Alashi
ASP.NET/IIS MVP
Jordev
Readify
www.DotNetArabi.com
www.EmadAshi.com
@emadashi
URL Routing & MVC
ALL YOUR URL ARE BELONG TO YOU!
Agenda
• Why understanding Routing is important
• What is Routing?
• How it works
• How to test it
• Best Some practices
Why understanding Routing
• The entry to your web app
• HyperTextTransferProtol
• Strongly relates to Binding
• Never stay in doubt
What is URL Routing
History
/store/products.aspx?key=value
                                     Store       Page life cycle




                                                  QueryString*“..”+
                                 Products.aspx
Handlers
Store/products/apples


•    Parse
•    Extract variables         OtherHttpHandler

•    Route to Handler    URL

                               MVCHttpHandler
Handlers
 Options?
• Rigid (absolute string comparison):
   e.g. “http://store/products/view” ===> invoke method “view” in class “products”


• Pattern base:
   1.   http://store/{classX}/{methodY} ===> use MvcHttpHandler => Invoke method “Y” in class “X”
   2.   http://p/sub}/{module} ===> use MagicHttpHandler => invoke crazy code
Priority
We have to choose between patterns!
           routes.MapRoute(
               name: "Default",
               url: "{controller}/{action}/",
               defaults: new { controller = "Home", action = "Index"}
           );
           routes.MapRoute(
               name: "My2ndRoute",
               url: "special{awesome}Pattern/{anotherVariable}",
               defaults: new { awesome = "magic"}
           );
How URL Routing
works
Segmentation
        http://store.com/home/index

                   First segment   Second segment
Static words
                   “,controller- /,action-”
                       “home/index”
                     “anything/willdo”
           =============================
               “abc,controller- /     ,action-”
                 “abchome / whatever”
                   ==================
               “abc,controller- /     ,action-”
                   “home / whatever”
RouteData.Values
  “,controller}/{action-/,id-”
  “product/index/3”



                                 Variable     value
                                 controller   Product
                                 action       Index
                                 id           3
Defaults
Or Defaults:                                        Variable     Value
     “product/index”                                controller   Product

?                                                   action       Index
     routes.MapRoute(                               id
                                                    Id           !
                                                                 3

               name: "Default",

               url: "{controller}/{action}/{id}",

               defaults: new { id=3 }

         );
UrlParameter.Optional
 routes.MapRoute(

 name: "Default",

 url: "{controller}/{action}/{id}",                          Variable     value
 defaults: new {action = "Index“, id=UrlParameter.Optinal}   controller   Product

             );                                              action       Index
                                                             id           3
• Supplied: “product/index/3”
• Not supplied:                                              Variable     value
       “product/index”                                       controller   Product
                                                             action       index
No excessive number of segments
          “,controller-/,action-/,id-”
          “products/oranges/edit/3”
Unless:
          “,controller}/{action}/{id}/{*catchall-”
          “product/oranges/edit/3/something’
                                                     Variable     value
                                                     controller   Product
                                                     action       Index
                                                     id           edit
                                                     Catchall     3/something
Constraints
 1.   Regular Expressions:
      routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", …

                    constraints: new { controller = "^H.*" }          );
 2.   Specific values:
                    constraints: new { action = "^Index$|^About$*" }
 3.   HTTP methods:
                    constraints: new { httpMethod= new HttpMethodConstraint("GET") }
 4.   Custom constraints:

      bool IRouteConstraint.Match(HttpContextBase, Route, stringParameterName, RouteValueDictionary,
      RouteDireciont)
Debug Routes
Notes (Incoming)
• Reserved custom variables: “controller, action, area”
• routes.RouteExistingFiles = true;
• routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
• Last segment includes the QueryString
How Routing works
   (Outgoing)
Helpers
1.   Html.ActionLink(text, nameOfAction, nameOfController, RouteValues, HtmlAttributes)
2.   Url.Action(text, nameOfAction, nameOfController, RouteValues)
3.   @Html.RouteLink(text, RouteValues, HtmlAttributes)
4.   Url.RouteLink(text, AnonymouseTypeValues)


•    You can use Route name
Rules
1.   All variables should have values: “,controller-/,action-/,id-”
     a)   Supplied
     b)   Current request
     c)   Default values

2.   Should not dis-agree with default-only variables:
     routes.MapRoute("MyRoute", "{controller}/{action}", new { myVar = "true" });

3.   Satisfy constraints
Notes (Outgoing)
• Tricky: Reuse values of the current request URL:
   url: "{controller}/{action}/{id}/{forth}",

                   defaults: new { controller = "Home", action = "Index", id = 3, forth=8},
------------
                  @Html.ActionLink("this is the link", "Index", new {id=5 });
                  @Html.ActionLink("this is the link", "Index", new {forth=8 });

• Url’s generated will try to produce the shortest url
Areas
Areas
public class AdminAreaRegistration : AreaRegistration {
       public override string AreaName         { get   {
                return "Admin";     }    }
       public override void RegisterArea(AreaRegistrationContext context)
       {
           context.MapRoute(
                "Admin_default",
                "Admin/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
           );
       }
   }
Areas
AreaRegistration.RegisterAllAreas();


RouteConfig.RegisterRoutes(RouteTable.Routes);
Unit-Testing Routes
Unit-Testing Routes (Incoming)
What do we want to test?


That the url patterns we want to support in our web app would populate the RouteData variables
as expected.
Unit-Testing Routes (Incoming)
• Incoming:
   •   HttpRequestBase
   •   HttpContextBase
   •   HttpResponseBase

• Outcoing:
   •   +
   •   UrlHelper
Unit-Testing Routes (Incoming)


                Demo
Unit-Testing Routes (Incoming)

                                     MvcContrib
                             http://mvccontrib.codeplex.com/


"~/Products/View/44/offer".ShouldMapTo<ProductsController>(action => action.View(44, “offer"));
Design Guidelines
• Content vs Implementation
• Human friendly: content titles vs id’s
• Hackable
• Sense of hierarchy
• Static strings at the beggning of routes
• Dash instead of underscore
• Not too long
• Avoid complexity
• Be consistent
ASP.NET

       It’s all open source!
     http://aspnetwebstack.codeplex.com/
Q&A


      www.emadashi.com
         @EmadAshi

More Related Content

PPTX
Presentation on "An Introduction to ReactJS"
PDF
Javascript and DOM
PDF
3. Java Script
PPT
Introduction to CSS
PPT
PDF
ES6 presentation
PPTX
Javascript 101
PDF
Javascript essentials
Presentation on "An Introduction to ReactJS"
Javascript and DOM
3. Java Script
Introduction to CSS
ES6 presentation
Javascript 101
Javascript essentials

What's hot (20)

PPT
CSS
PDF
React JS - Introduction
PPT
Angular 8
PPT
Introduction to Javascript
PDF
Intro to HTML & CSS
PDF
Basics of JavaScript
PPTX
An Introduction to the DOM
PPTX
PDF
An introduction to React.js
PPT
JavaScript - An Introduction
PPT
Cascading Style Sheets (CSS) help
PDF
Introduction to JavaScript
PPT
Javascript
PPTX
Angular
PDF
jQuery for beginners
PDF
React js
PPTX
Intro to React
PPTX
PPTX
JSON: The Basics
CSS
React JS - Introduction
Angular 8
Introduction to Javascript
Intro to HTML & CSS
Basics of JavaScript
An Introduction to the DOM
An introduction to React.js
JavaScript - An Introduction
Cascading Style Sheets (CSS) help
Introduction to JavaScript
Javascript
Angular
jQuery for beginners
React js
Intro to React
JSON: The Basics
Ad

Similar to ASP.NET Routing & MVC (20)

PPTX
Murach : HOW to work with controllers and routing
PPTX
Asp.Net Mvc Internals &amp; Extensibility
PPTX
Mvc3 part2
PDF
Asp.net mvc internals & extensibility
DOC
Custom routing in asp.net mvc
PPT
ASP .net MVC
PPTX
Chapter5.pptx
PPTX
Asp.Net Mvc
PDF
Custom URL Re-Writing/Routing using Attribute Routes in MVC 4 Web APIs
PPTX
Build your web app with asp.net mvc 2 from scratch
PPT
ASP.NET MVC introduction
PPTX
PDF
ASP.NET MVC - Whats The Big Deal
PPTX
Web api routing
PDF
PPTX
Asp.Net MVC3 - Basics
ODP
Rails training presentation routing
PDF
Asp.Net MVC Framework Design Pattern
PPTX
Routing
PDF
Spring Mvc Rest
Murach : HOW to work with controllers and routing
Asp.Net Mvc Internals &amp; Extensibility
Mvc3 part2
Asp.net mvc internals & extensibility
Custom routing in asp.net mvc
ASP .net MVC
Chapter5.pptx
Asp.Net Mvc
Custom URL Re-Writing/Routing using Attribute Routes in MVC 4 Web APIs
Build your web app with asp.net mvc 2 from scratch
ASP.NET MVC introduction
ASP.NET MVC - Whats The Big Deal
Web api routing
Asp.Net MVC3 - Basics
Rails training presentation routing
Asp.Net MVC Framework Design Pattern
Routing
Spring Mvc Rest
Ad

More from Emad Alashi (12)

PPTX
RBAC in Azure Kubernetes Service AKS
PPTX
Am I a Good Developer
PPTX
Basic Intro to WinDbg
PPTX
Acquiring knowledge
PPTX
Owin, Katana, and Helios
PPTX
OAuth in the new .NET world (OWIN)
PPTX
Software Life Cycle, Humans & Code
PPTX
HTML5 & IE
PPTX
ASP.NET MVC One Step Deeper
PPTX
Introduction to ASP.NET MVC
PPTX
Communication Skills one To one
PPS
Introduction To NHibernate
RBAC in Azure Kubernetes Service AKS
Am I a Good Developer
Basic Intro to WinDbg
Acquiring knowledge
Owin, Katana, and Helios
OAuth in the new .NET world (OWIN)
Software Life Cycle, Humans & Code
HTML5 & IE
ASP.NET MVC One Step Deeper
Introduction to ASP.NET MVC
Communication Skills one To one
Introduction To NHibernate

Recently uploaded (20)

PDF
Spectral efficient network and resource selection model in 5G networks
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Encapsulation theory and applications.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Approach and Philosophy of On baking technology
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPT
Teaching material agriculture food technology
PPTX
MYSQL Presentation for SQL database connectivity
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Machine learning based COVID-19 study performance prediction
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Machine Learning_overview_presentation.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
A comparative analysis of optical character recognition models for extracting...
PPTX
Spectroscopy.pptx food analysis technology
PPTX
A Presentation on Artificial Intelligence
Spectral efficient network and resource selection model in 5G networks
NewMind AI Weekly Chronicles - August'25-Week II
Encapsulation theory and applications.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Approach and Philosophy of On baking technology
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Teaching material agriculture food technology
MYSQL Presentation for SQL database connectivity
MIND Revenue Release Quarter 2 2025 Press Release
Machine learning based COVID-19 study performance prediction
Digital-Transformation-Roadmap-for-Companies.pptx
20250228 LYD VKU AI Blended-Learning.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
The Rise and Fall of 3GPP – Time for a Sabbatical?
Encapsulation_ Review paper, used for researhc scholars
Machine Learning_overview_presentation.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
A comparative analysis of optical character recognition models for extracting...
Spectroscopy.pptx food analysis technology
A Presentation on Artificial Intelligence

ASP.NET Routing & MVC

  • 2. URL Routing & MVC ALL YOUR URL ARE BELONG TO YOU!
  • 3. Agenda • Why understanding Routing is important • What is Routing? • How it works • How to test it • Best Some practices
  • 4. Why understanding Routing • The entry to your web app • HyperTextTransferProtol • Strongly relates to Binding • Never stay in doubt
  • 5. What is URL Routing
  • 6. History /store/products.aspx?key=value Store Page life cycle QueryString*“..”+ Products.aspx
  • 7. Handlers Store/products/apples • Parse • Extract variables OtherHttpHandler • Route to Handler URL MVCHttpHandler
  • 8. Handlers Options? • Rigid (absolute string comparison): e.g. “http://store/products/view” ===> invoke method “view” in class “products” • Pattern base: 1. http://store/{classX}/{methodY} ===> use MvcHttpHandler => Invoke method “Y” in class “X” 2. http://p/sub}/{module} ===> use MagicHttpHandler => invoke crazy code
  • 9. Priority We have to choose between patterns! routes.MapRoute( name: "Default", url: "{controller}/{action}/", defaults: new { controller = "Home", action = "Index"} ); routes.MapRoute( name: "My2ndRoute", url: "special{awesome}Pattern/{anotherVariable}", defaults: new { awesome = "magic"} );
  • 11. Segmentation http://store.com/home/index First segment Second segment
  • 12. Static words “,controller- /,action-” “home/index” “anything/willdo” ============================= “abc,controller- / ,action-” “abchome / whatever” ================== “abc,controller- / ,action-” “home / whatever”
  • 13. RouteData.Values “,controller}/{action-/,id-” “product/index/3” Variable value controller Product action Index id 3
  • 14. Defaults Or Defaults: Variable Value “product/index” controller Product ? action Index routes.MapRoute( id Id ! 3 name: "Default", url: "{controller}/{action}/{id}", defaults: new { id=3 } );
  • 15. UrlParameter.Optional routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", Variable value defaults: new {action = "Index“, id=UrlParameter.Optinal} controller Product ); action Index id 3 • Supplied: “product/index/3” • Not supplied: Variable value “product/index” controller Product action index
  • 16. No excessive number of segments “,controller-/,action-/,id-” “products/oranges/edit/3” Unless: “,controller}/{action}/{id}/{*catchall-” “product/oranges/edit/3/something’ Variable value controller Product action Index id edit Catchall 3/something
  • 17. Constraints 1. Regular Expressions: routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", … constraints: new { controller = "^H.*" } ); 2. Specific values: constraints: new { action = "^Index$|^About$*" } 3. HTTP methods: constraints: new { httpMethod= new HttpMethodConstraint("GET") } 4. Custom constraints: bool IRouteConstraint.Match(HttpContextBase, Route, stringParameterName, RouteValueDictionary, RouteDireciont)
  • 19. Notes (Incoming) • Reserved custom variables: “controller, action, area” • routes.RouteExistingFiles = true; • routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); • Last segment includes the QueryString
  • 20. How Routing works (Outgoing)
  • 21. Helpers 1. Html.ActionLink(text, nameOfAction, nameOfController, RouteValues, HtmlAttributes) 2. Url.Action(text, nameOfAction, nameOfController, RouteValues) 3. @Html.RouteLink(text, RouteValues, HtmlAttributes) 4. Url.RouteLink(text, AnonymouseTypeValues) • You can use Route name
  • 22. Rules 1. All variables should have values: “,controller-/,action-/,id-” a) Supplied b) Current request c) Default values 2. Should not dis-agree with default-only variables: routes.MapRoute("MyRoute", "{controller}/{action}", new { myVar = "true" }); 3. Satisfy constraints
  • 23. Notes (Outgoing) • Tricky: Reuse values of the current request URL: url: "{controller}/{action}/{id}/{forth}", defaults: new { controller = "Home", action = "Index", id = 3, forth=8}, ------------ @Html.ActionLink("this is the link", "Index", new {id=5 }); @Html.ActionLink("this is the link", "Index", new {forth=8 }); • Url’s generated will try to produce the shortest url
  • 24. Areas
  • 25. Areas public class AdminAreaRegistration : AreaRegistration { public override string AreaName { get { return "Admin"; } } public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( "Admin_default", "Admin/{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional } ); } }
  • 28. Unit-Testing Routes (Incoming) What do we want to test? That the url patterns we want to support in our web app would populate the RouteData variables as expected.
  • 29. Unit-Testing Routes (Incoming) • Incoming: • HttpRequestBase • HttpContextBase • HttpResponseBase • Outcoing: • + • UrlHelper
  • 31. Unit-Testing Routes (Incoming) MvcContrib http://mvccontrib.codeplex.com/ "~/Products/View/44/offer".ShouldMapTo<ProductsController>(action => action.View(44, “offer"));
  • 32. Design Guidelines • Content vs Implementation • Human friendly: content titles vs id’s • Hackable • Sense of hierarchy • Static strings at the beggning of routes • Dash instead of underscore • Not too long • Avoid complexity • Be consistent
  • 33. ASP.NET It’s all open source! http://aspnetwebstack.codeplex.com/
  • 34. Q&A www.emadashi.com @EmadAshi

Editor's Notes

  • #3: How many dealt with ASP.NET and HttpHandler
  • #7: Some historyA page is an HttpHandlerToo rigidBound to a fileThis will not work for MVC, they need to invoke Methods (Actions) in Classes (Controller)So they wanted more flexibilityFile/extension agnosticDifferent handlers (especially they needed to introduce the separation of concerns and invoking controllers on certain points “actions”)
  • #8: When a request comes to the server, how can the app know how to handle this request?Emphasize on RouteData being passed to the handler
  • #9: So we could be rigid, or dynamicBut how can we make programmers more interesting and harder? :P
  • #10: At the startup of the web app
  • #11: It’s hard to put it in steps since it’s little bit complex and wined together, but we will try
  • #16: “To be clear, it is not that the value of id is null when nocorresponding segment is supplied; rather, the case is that an id variable is not defined”To distinguish if user sent a value or notSeparation of concerns (defaults in routing?)
  • #18: Specific values for controllers that might share a URL patternHttp Method has nothing to do with the “post” and “get” action filters
  • #19: Inspired by RouteDebugger created by Phil Haack
  • #20: Last segment includes the query string, but it’s up to the IHttpHandler how to handle it
  • #22: Don’t use fixed URL’s!Routing doesn’t understand what “controller” is or what “action” is.Explain parametersExtra values are added as aquery stringThe Html.Action method uses routing as well, it’s not a direct call
  • #24: Reuse values of the current request URL only for segment variables that occur earlier in the URL pattern than any parameters that are supplied to the Html.ActionLink method:
  • #26: Don’t change namespace of controller in an AreaDo use namespace priority for the general controller
  • #27: Don’t change the order of Area registration to be after Routing
  • #29: Because it’s the key for the MVCHttpHandlerBy: checking the orderChecking the static stringsChecking constraints if we have them
  • #30: Before, it was hard to test Routes, now they provided HttpContextBase
  • #31: Before, it was hard to test Routes, now they provided HttpContextBase
  • #32: “offer” might be date as wellI am not totally enthusiast since this is not testing Routing only, it intervenes of how binding works
  • #33: I don’t like best practicesIf external website maybe you want to follow the SEOAvoid complexityContent vs Implementation: REST or Action? if easy go RESTful, no just don’t not too longContent title’s vs ID’s: avoid ID’s in general
  • #34: For me, sometimes Reflector is easier to navigate