Your SlideShare is downloading. ×
ASP.NET MVC 4 - Routing Internals
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

ASP.NET MVC 4 - Routing Internals

6,345

Published on

Presentation from 2nd meeting of ASP.NET MVC 4 Study Group. It containts: …

Presentation from 2nd meeting of ASP.NET MVC 4 Study Group. It containts:
1. Route registration (also for areas).
2. How routing works?
3. URL Generation.
4. Route Debugging.

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
6,345
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
123
Comments
0
Likes
3
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Routing Internals Łukasz Łysik ASP.NET MVC 4 Study Group
  • 2. Agenda • Routes Registration • Routes Registration for Areas • How routing works? • Generating URLs • Debugging routes
  • 3. ROUTES REGISTRATION
  • 4. Routes Registration Overview Are there any restrictions here?
  • 5. Routes Registration Overview
  • 6. Routes Registration MapRoute RouteCollectionExtensions:
  • 7. Routes Registration Route class
  • 8. MySite/{controller}-{action}/{id} Routes Registration Route class ParsedRoute ParsedRoute ContentPathSegment LiteralSubsegment („MySite”) SeparatorPathSegment ContentPathSegment ParameterSubsegment {controller} LiteralSubsegment „-” ParameterSubsegment {action} SeparatorPathSegment ContentPathSegment ParameterSubsegments {id}
  • 9. Routes Registration Route class Constraints
  • 10. Routes Registration Route class Custom Constraints Problem: We want to have mechanism which closes our web site for maintenance for specified period of time. Inputs:
  • 11. Routes Registration Route class Custom Constraints Coding
  • 12. Routes Registration Route class Custom Constraints
  • 13. Routes Registration Route class Custom Constraints
  • 14. Routes Registration Route class DataTokens • Contain values associated with the route that are not used when ASP.NET routing determines whether a route matches a request. • Custom data tokens can be defined and used by custom logic in route handler, controller, action, etc. • Predefined DataTokens are related to Areas.
  • 15. ROUTES REGISTRATION FOR AREAS
  • 16. Routes Registration for Areas Overview
  • 17. Routes Registration for Areas Overview Problem: Multiple controllers with the same names
  • 18. Routes Registration for Areas MapRoute
  • 19. Routes Registration for Areas DataTokens collection Area Current area name. Namespaces Namespaces to search for controllers. UseNamespaceFallback Set to „false” to disable searching in other namespaces. Q: In which part of request pipeline these two DataTokens are used? A: In DefaultControllerFactory.
  • 20. Routes Registration for Areas Usage of DataTokens DefaultControllerFactory:
  • 21. HOW ROUTING WORKS?
  • 22. Route 1 URL 1 ParsedRoute 1 Defaults 1 Constraints 1 DataTokens 1 RouteHandler 1 Route 2 URL 2 ParsedRoute 2 Defaults 2 Constraints 2 DataTokens 2 RouteHandler 2 … Route n URL n ParsedRoute n Defaults n Constraints n DataTokens n RouteHandler n How Routing Works? RouteTable.Routes
  • 23. How Routing Works? Routing Process (1) UrlRoutingModule RouteTable.Routes Do you have any routes for this context? Route 1 Route 2 Route n . . . ParsedRoute matches current URL? Do I have any constraints? Process constraints. Constraints satisfied? Return null.Build and return RouteData. No No No Yes Yes
  • 24. How Routing Works? Routing Process (2) UrlRoutingModule RouteData (based on Route 2) Give me IRouteHandler IRouteHandler Give me IHttpHandler Tell IIS to use IHttpHandler which I got from matched Route.
  • 25. How Routing Works? RouteData
  • 26. How Routing Works? Route.ProcessConstraint
  • 27. How Routing Works? Route.GetRouteData
  • 28. How Routing Works? Route.ProcessConstraint
  • 29. URL GENERATION
  • 30. URL Generation Overview
  • 31. URL Generation Examples (1)
  • 32. URL Generation Examples (2)
  • 33. URL Generation Matching Algorithm RouteCollection.GetVirtualPath(Supplied values) Does Route have required parameters? Did the call to GetVirtualPath specify a value for each required parameter? Does Route have default values that do not correspond to URL parameter? Does true value for that default, if specified, match the specified value? Process constraints Yes Yes Yes No Required = with no default value No match! No No URL: “todo/{action}” Defaults: controller=home action=index No match! No Yes
  • 34. DEBUGGING ROUTES
  • 35. • Possible problems: – Routes don’t work. – URL is routed to wrong controller/action. – DataTokens values are missing (are wrong). – Constraints don’t work. • What we need? – Tool that will tell us which routes are being matched, what RouteData parameters and DataTokens are. Debugging Routes Problems
  • 36. Debugging Routes How? Q: How to write a tool that will debug routes? Q: What is the name of static class which stores routes? Q: Which request pipeline element uses RouteTable.Routes? A: RouteTable.Routes A: UrlRoutingModule
  • 37. Demo Debugging Routes Coding session
  • 38. Debugging Routes Coding session
  • 39. • RouteDebugger by Phil Haack: – http://haacked.com/archive/20 11/04/12/routedebugger- 2.aspx • How it works: – Registers own HTTP module. – Attach to Request_End event. – Writes route data at the bottom of each view. Debugging Routes RouteDebugger 2.0
  • 40. References Professional ASP.NET MVC 4 (http://www.amazon.co.uk/Professional-ASP-NET-MVC-Wrox-Guides/dp/111834846X)
  • 41. Questions?

×