asp.net mvc 4 - routing internals
DESCRIPTION
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.TRANSCRIPT
![Page 1: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/1.jpg)
Routing Internals
Łukasz ŁysikASP.NET MVC 4 Study Group
![Page 2: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/2.jpg)
Agenda
• Routes Registration• Routes Registration for Areas• How routing works?• Generating URLs• Debugging routes
![Page 3: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/3.jpg)
ROUTES REGISTRATION
![Page 4: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/4.jpg)
Routes Registration Overview
Are there any restrictions here?
![Page 5: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/5.jpg)
Routes Registration Overview
![Page 6: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/6.jpg)
Routes Registration MapRoute
RouteCollectionExtensions:
![Page 7: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/7.jpg)
Routes Registration Route class
![Page 8: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/8.jpg)
MySite/{controller}-{action}/{id}
Routes Registration Route class ParsedRoute
ParsedRoute
ContentPathSegment
LiteralSubsegment („MySite”)
SeparatorPathSegment ContentPathSegment
ParameterSubsegment {controller}
LiteralSubsegment „-”
ParameterSubsegment {action}
SeparatorPathSegment ContentPathSegment
ParameterSubsegments {id}
![Page 9: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/9.jpg)
Routes Registration Route class Constraints
![Page 10: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/10.jpg)
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:
![Page 11: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/11.jpg)
Routes Registration Route class Custom
Constraints
Coding
![Page 12: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/12.jpg)
Routes Registration Route class Custom
Constraints
![Page 13: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/13.jpg)
Routes Registration Route class Custom
Constraints
![Page 14: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/14.jpg)
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.
![Page 15: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/15.jpg)
ROUTES REGISTRATION FOR AREAS
![Page 16: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/16.jpg)
Routes Registration for Areas Overview
![Page 17: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/17.jpg)
Routes Registration for Areas Overview
Problem: Multiple controllers with the same names
![Page 18: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/18.jpg)
Routes Registration for Areas MapRoute
![Page 19: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/19.jpg)
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.
![Page 20: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/20.jpg)
Routes Registration for Areas Usage of DataTokens
DefaultControllerFactory:
![Page 21: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/21.jpg)
HOW ROUTING WORKS?
![Page 22: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/22.jpg)
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
![Page 23: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/23.jpg)
How Routing Works? Routing Process (1)
UrlRoutingModule RouteTable.RoutesDo 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
NoNo
Yes
Yes
![Page 24: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/24.jpg)
How Routing Works? Routing Process (2)
UrlRoutingModule RouteData(based on Route 2)
Give me IRouteHandler
IRouteHandlerGive me IHttpHandler
Tell IIS to use IHttpHandler which I
got from matched Route.
![Page 25: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/25.jpg)
How Routing Works? RouteData
![Page 26: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/26.jpg)
How Routing Works? Route.ProcessConstraint
![Page 27: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/27.jpg)
How Routing Works? Route.GetRouteData
![Page 28: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/28.jpg)
How Routing Works? Route.ProcessConstraint
![Page 29: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/29.jpg)
URL GENERATION
![Page 30: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/30.jpg)
URL Generation Overview
![Page 31: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/31.jpg)
URL Generation Examples (1)
![Page 32: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/32.jpg)
URL Generation Examples (2)
![Page 33: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/33.jpg)
URL Generation Matching AlgorithmRouteCollection.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
NoRequired = with no default value
No match!No
No
URL: “todo/{action}”Defaults: controller=home action=index
No match!No
Yes
![Page 34: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/34.jpg)
DEBUGGING ROUTES
![Page 35: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/35.jpg)
• 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
![Page 36: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/36.jpg)
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
![Page 37: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/37.jpg)
Demo
Debugging Routes Coding session
![Page 38: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/38.jpg)
Debugging Routes Coding session
![Page 39: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/39.jpg)
• RouteDebugger by Phil Haack:– http://
haacked.com/archive/2011/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
![Page 40: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/40.jpg)
References
Professional ASP.NET MVC 4(http://www.amazon.co.uk/Professional-ASP-NET-MVC-Wrox-Guides/dp/111834846X)
![Page 41: ASP.NET MVC 4 - Routing Internals](https://reader033.vdocuments.us/reader033/viewer/2022052505/5555d3ecd8b42aaf158b4e65/html5/thumbnails/41.jpg)
Questions?