SlideShare una empresa de Scribd logo
1 de 21
ASP.NET ROUTING
DEEP DIVE
Stacy Vicknair
Sparkhound
0
Welcome to Houston TechFest
Thank you for being a part of the
7th Annual Houston TechFest!
• Please turn off all electronic devices or set them to vibrate.
• If you must take a phone call, please do so in the lobby so as not
to disturb others.
• Thanks to our Diamond Sponsors:

1
Information
• Speaker presentation slides will be available at
www.houstontechfest.org within a week
• Don’t forget to complete the Bingo card to be
eligible for door prizes

2
ABOUT ME
Stacy Vicknair, MVP
Senior Development Consultant
Blog
http://www.wtfnext.com
Toastmasters
http://www.toastmasters.org
http://www.batonrougespeaks.com

Twitter: @svickn
Email: stacy.vicknair@sparkhound.com
“BE A HERO. GROW A BEARD.”
The Sparkhound Foundation is sponsoring the growth of
facial hair to raise Prostate Cancer Awareness.
Through Septembeard we will
raise funds for the research and
treatment of prostate cancer.

Want to join us? Learn more at Septembeard.org.
Agenda
•
•
•
•
•
•

What Is Routing?
Out of the Box ASP.NET MVC Routing
Customizing ASP.NET MVC Routing
Unit Testing ASP.NET MVC Routes
Routing In ASP.NET WebForms
Helpful Links

5
What is ASP.NET Routing?
“ASP.NET routing enables you to use URLs that do not have
to map to specific files in a Web site.”
- http://msdn.microsoft.com/en-us/library/cc668201.aspx

www.mysite.com/categories.aspx?categoryName=sports
www.mysite.com/categories/sports

6

6
HttpModules and HttpHandlers

7
ASP.NET Routing (System.Web.Routing)

8
ASP.NET MVC Routing (System.Web.Mvc)

9
Out of the box MVC Routing
• Routes.IgnoreRoute()
– System.Web.Routing.StopRoutingHandler

• Routes.MapRoute()
– System.Web.Mvc.RouteCollectionExtensions
– Parameters

1
0

• string name – “MyUniqueName”
• string url – “{controller}/{action}/{id}”
• Object defaults new { controller = "Home", action = "Index”}
• Object constraints – new {id = “[0-9]+”}
• string[] namespaces - new[] { "MyProject.MyControllers"}
10
DEMO – Out of the box MVC Routing

1
1

11
Customizing ASP.NET MVC Routing
• Three main areas for routing customization
– RouteConstraints
• Implementing constraints that require more logic than RegEx
• Created by implementing System.Web.Routing.IRouteConstraint

– Routes
• Allow for control over how route data is parsed or route paths are
determined
• Created by subclassing System.Web.Routing.RouteBase
• Default is Route ex. RouteCollection.Add(new Route(…))

– RouteHandlers
• Allow for preprocessing before default RouteHandler behavior
– ex. RouteData modifications
12
DEMO – RouteConstraints and Routes

1
3

13
Unit Testing Routes
• Use RouteTable.Routes.GetRouteData()
– Requires mocked HttpContext (to provide the URL)

• Enumerate over RouteData to ensure the proper
information is returned
• Alternative is MVCContrib
– string.ShouldMapTo<T>(c => c.Action());
– Not “officially” ported to MVC4 by Ndepend yet.
• Search NuGet for MvcContrib.Mvc4 to find a port

14
DEMO – Unit Testing with Routes

1
5

15
Adding Routing to ASP.NET WebForms
• In the Global.asax, add a reference to System.Web.Routing
• Add routes via RouteTable.Routes.MapPageRoute()
• Update links to use routes
– RouteUrl
– GetVirtualPath

• Get Routing Data
– RouteValue
– RouteData.Values[]

16
DEMO – Routes in ASP.NET WebForms

1
7

17
Agenda
•
•
•
•
•
•

What Is Routing?
Out of the Box ASP.NET MVC Routing
Customizing ASP.NET MVC Routing
Unit Testing ASP.NET MVC Routes
Routing In ASP.NET WebForms
Helpful Links

18
Helpful Links
THE MOST IMPORTANT LINK I COULD EVER TELL YOU:
http://aspnetwebstack.codeplex.com/
http://mvccontrib.codeplex.com/ - MVCContrib
http://msdn.microsoft.com/en-us/library/dd329551.aspx - Webforms
Google or Bing: ASP.NET Routing

Slides / Code will be available at http://www.wtfnext.com
References:
http://www.15seconds.com/Issue/020417.htm
http://msdn.microsoft.com/en-us/library/cc668201.aspx
http://blogs.msdn.com/b/alikl/archive/2009/04/02/how-asp-net-mvc-works-for-aspiring-architects-2.aspx
http://blog.didsburydesign.com/2010/02/how-to-allow-hyphens-in-urls-using-asp-net-mvc-2/
http://stackoverflow.com/questions/1107507/asp-net-mvc-custom-route-handler-constraint
http://haacked.com/archive/2007/12/17/testing-routes-in-asp.net-mvc.aspx

19
Please Leave Feedback During Q&A
If you leave session
feedback and
provide contact
information, you
will be qualified for
a prize
Scan the QR code
to the right or go to
bit.ly/htf130202
20

Más contenido relacionado

Similar a ASP.NET Routing Deep Dive

Similar a ASP.NET Routing Deep Dive (20)

Who's Watching, by Geoff Huston [APNIC 38 / Technical Keynote]
Who's Watching, by Geoff Huston [APNIC 38 / Technical Keynote]Who's Watching, by Geoff Huston [APNIC 38 / Technical Keynote]
Who's Watching, by Geoff Huston [APNIC 38 / Technical Keynote]
 
Mulesoft Pune Meetup Deck - Apr 2020
Mulesoft Pune Meetup Deck - Apr 2020Mulesoft Pune Meetup Deck - Apr 2020
Mulesoft Pune Meetup Deck - Apr 2020
 
VTA Hack My Ride Lightning Talks
VTA Hack My Ride Lightning TalksVTA Hack My Ride Lightning Talks
VTA Hack My Ride Lightning Talks
 
Jinchao demo v3
Jinchao demo v3Jinchao demo v3
Jinchao demo v3
 
Jetstream: Adding Cloud-based Computing to the National Cyberinfrastructure
Jetstream: Adding Cloud-based Computing to the National CyberinfrastructureJetstream: Adding Cloud-based Computing to the National Cyberinfrastructure
Jetstream: Adding Cloud-based Computing to the National Cyberinfrastructure
 
Slides internet technology
Slides internet technologySlides internet technology
Slides internet technology
 
Jetstream - Adding Cloud-based Computing to the National Cyberinfrastructure
Jetstream - Adding Cloud-based Computing to the National CyberinfrastructureJetstream - Adding Cloud-based Computing to the National Cyberinfrastructure
Jetstream - Adding Cloud-based Computing to the National Cyberinfrastructure
 
Building Real Time Web Applications with SignalR (NoVA Code Camp 2015)
Building Real Time Web Applications with SignalR (NoVA Code Camp 2015)Building Real Time Web Applications with SignalR (NoVA Code Camp 2015)
Building Real Time Web Applications with SignalR (NoVA Code Camp 2015)
 
Java socket programming
Java socket programmingJava socket programming
Java socket programming
 
Power to the People: Manipulating SharePoint with Client-Side JavaScript
Power to the People:  Manipulating SharePoint with Client-Side JavaScriptPower to the People:  Manipulating SharePoint with Client-Side JavaScript
Power to the People: Manipulating SharePoint with Client-Side JavaScript
 
Web Socket
Web SocketWeb Socket
Web Socket
 
Scalable IoT platform
Scalable IoT platformScalable IoT platform
Scalable IoT platform
 
SPS Chevy Chase - Build It and They Will Come: Sharepoint 2013 User Adoption
SPS Chevy Chase - Build It and They Will Come: Sharepoint 2013 User AdoptionSPS Chevy Chase - Build It and They Will Come: Sharepoint 2013 User Adoption
SPS Chevy Chase - Build It and They Will Come: Sharepoint 2013 User Adoption
 
MyIX Updates
MyIX UpdatesMyIX Updates
MyIX Updates
 
Sqlviking
SqlvikingSqlviking
Sqlviking
 
Integrating volunteers and Experts
Integrating volunteers and ExpertsIntegrating volunteers and Experts
Integrating volunteers and Experts
 
Real-Time Web Apps & .NET. What Are Your Options? NDC Oslo 2016
Real-Time Web Apps & .NET. What Are Your Options? NDC Oslo 2016Real-Time Web Apps & .NET. What Are Your Options? NDC Oslo 2016
Real-Time Web Apps & .NET. What Are Your Options? NDC Oslo 2016
 
Introduction to WebSockets
Introduction to WebSocketsIntroduction to WebSockets
Introduction to WebSockets
 
Windows 8 Metro apps and the outside world
Windows 8 Metro apps and the outside worldWindows 8 Metro apps and the outside world
Windows 8 Metro apps and the outside world
 
CCNA RSE Routing concept
CCNA RSE Routing conceptCCNA RSE Routing concept
CCNA RSE Routing concept
 

Más de Stacy Vicknair

Más de Stacy Vicknair (8)

Systems Thinking for Engineering Leaders.pdf
Systems Thinking for Engineering Leaders.pdfSystems Thinking for Engineering Leaders.pdf
Systems Thinking for Engineering Leaders.pdf
 
The Game Master's Guide to People Management
The Game Master's Guide to People ManagementThe Game Master's Guide to People Management
The Game Master's Guide to People Management
 
Maintainable Documentation with Sphinx
Maintainable Documentation with SphinxMaintainable Documentation with Sphinx
Maintainable Documentation with Sphinx
 
Building Faster Horses: Taking Over An Existing Software Product
Building Faster Horses: Taking Over An Existing Software ProductBuilding Faster Horses: Taking Over An Existing Software Product
Building Faster Horses: Taking Over An Existing Software Product
 
Git'in in 15
Git'in in 15Git'in in 15
Git'in in 15
 
Git'in on Windows
Git'in on WindowsGit'in on Windows
Git'in on Windows
 
ASP.NET Routing Deep Dive
ASP.NET Routing Deep DiveASP.NET Routing Deep Dive
ASP.NET Routing Deep Dive
 
Asp.net routing with mvc deep dive
Asp.net routing with mvc deep diveAsp.net routing with mvc deep dive
Asp.net routing with mvc deep dive
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 

ASP.NET Routing Deep Dive

  • 1. ASP.NET ROUTING DEEP DIVE Stacy Vicknair Sparkhound 0
  • 2. Welcome to Houston TechFest Thank you for being a part of the 7th Annual Houston TechFest! • Please turn off all electronic devices or set them to vibrate. • If you must take a phone call, please do so in the lobby so as not to disturb others. • Thanks to our Diamond Sponsors: 1
  • 3. Information • Speaker presentation slides will be available at www.houstontechfest.org within a week • Don’t forget to complete the Bingo card to be eligible for door prizes 2
  • 4. ABOUT ME Stacy Vicknair, MVP Senior Development Consultant Blog http://www.wtfnext.com Toastmasters http://www.toastmasters.org http://www.batonrougespeaks.com Twitter: @svickn Email: stacy.vicknair@sparkhound.com
  • 5. “BE A HERO. GROW A BEARD.” The Sparkhound Foundation is sponsoring the growth of facial hair to raise Prostate Cancer Awareness. Through Septembeard we will raise funds for the research and treatment of prostate cancer. Want to join us? Learn more at Septembeard.org.
  • 6. Agenda • • • • • • What Is Routing? Out of the Box ASP.NET MVC Routing Customizing ASP.NET MVC Routing Unit Testing ASP.NET MVC Routes Routing In ASP.NET WebForms Helpful Links 5
  • 7. What is ASP.NET Routing? “ASP.NET routing enables you to use URLs that do not have to map to specific files in a Web site.” - http://msdn.microsoft.com/en-us/library/cc668201.aspx www.mysite.com/categories.aspx?categoryName=sports www.mysite.com/categories/sports 6 6
  • 10. ASP.NET MVC Routing (System.Web.Mvc) 9
  • 11. Out of the box MVC Routing • Routes.IgnoreRoute() – System.Web.Routing.StopRoutingHandler • Routes.MapRoute() – System.Web.Mvc.RouteCollectionExtensions – Parameters 1 0 • string name – “MyUniqueName” • string url – “{controller}/{action}/{id}” • Object defaults new { controller = "Home", action = "Index”} • Object constraints – new {id = “[0-9]+”} • string[] namespaces - new[] { "MyProject.MyControllers"} 10
  • 12. DEMO – Out of the box MVC Routing 1 1 11
  • 13. Customizing ASP.NET MVC Routing • Three main areas for routing customization – RouteConstraints • Implementing constraints that require more logic than RegEx • Created by implementing System.Web.Routing.IRouteConstraint – Routes • Allow for control over how route data is parsed or route paths are determined • Created by subclassing System.Web.Routing.RouteBase • Default is Route ex. RouteCollection.Add(new Route(…)) – RouteHandlers • Allow for preprocessing before default RouteHandler behavior – ex. RouteData modifications 12
  • 14. DEMO – RouteConstraints and Routes 1 3 13
  • 15. Unit Testing Routes • Use RouteTable.Routes.GetRouteData() – Requires mocked HttpContext (to provide the URL) • Enumerate over RouteData to ensure the proper information is returned • Alternative is MVCContrib – string.ShouldMapTo<T>(c => c.Action()); – Not “officially” ported to MVC4 by Ndepend yet. • Search NuGet for MvcContrib.Mvc4 to find a port 14
  • 16. DEMO – Unit Testing with Routes 1 5 15
  • 17. Adding Routing to ASP.NET WebForms • In the Global.asax, add a reference to System.Web.Routing • Add routes via RouteTable.Routes.MapPageRoute() • Update links to use routes – RouteUrl – GetVirtualPath • Get Routing Data – RouteValue – RouteData.Values[] 16
  • 18. DEMO – Routes in ASP.NET WebForms 1 7 17
  • 19. Agenda • • • • • • What Is Routing? Out of the Box ASP.NET MVC Routing Customizing ASP.NET MVC Routing Unit Testing ASP.NET MVC Routes Routing In ASP.NET WebForms Helpful Links 18
  • 20. Helpful Links THE MOST IMPORTANT LINK I COULD EVER TELL YOU: http://aspnetwebstack.codeplex.com/ http://mvccontrib.codeplex.com/ - MVCContrib http://msdn.microsoft.com/en-us/library/dd329551.aspx - Webforms Google or Bing: ASP.NET Routing Slides / Code will be available at http://www.wtfnext.com References: http://www.15seconds.com/Issue/020417.htm http://msdn.microsoft.com/en-us/library/cc668201.aspx http://blogs.msdn.com/b/alikl/archive/2009/04/02/how-asp-net-mvc-works-for-aspiring-architects-2.aspx http://blog.didsburydesign.com/2010/02/how-to-allow-hyphens-in-urls-using-asp-net-mvc-2/ http://stackoverflow.com/questions/1107507/asp-net-mvc-custom-route-handler-constraint http://haacked.com/archive/2007/12/17/testing-routes-in-asp.net-mvc.aspx 19
  • 21. Please Leave Feedback During Q&A If you leave session feedback and provide contact information, you will be qualified for a prize Scan the QR code to the right or go to bit.ly/htf130202 20

Notas del editor

  1. Why are we doing this? In honor of the Sparkies and Sparkie family members who have (or continue to fight) fought prostate cancerBackground:- The Sparkhound Foundation is our non-profit charitable giving organization- Prostate cancer is the second leading cause of death in American men.
  2. Why are we doing this? In honor of the Sparkies and Sparkie family members who have (or continue to fight) fought prostate cancerBackground:- The Sparkhound Foundation is our non-profit charitable giving organization- Prostate cancer is the second leading cause of death in American men.