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 electroni...
Information
• Speaker presentation slides will be available at
www.houstontechfest.org within a week
• Don’t forget to com...
ABOUT ME
Stacy Vicknair, MVP
Senior Development Consultant
Blog
http://www.wtfnext.com
Toastmasters
http://www.toastmaster...
“BE A HERO. GROW A BEARD.”
The Sparkhound Foundation is sponsoring the growth of
facial hair to raise Prostate Cancer Awar...
Agenda
•
•
•
•
•
•

What Is Routing?
Out of the Box ASP.NET MVC Routing
Customizing ASP.NET MVC Routing
Unit Testing ASP.N...
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....
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.We...
DEMO – Out of the box MVC Routing

1
1

11
Customizing ASP.NET MVC Routing
• Three main areas for routing customization
– RouteConstraints
• Implementing constraints...
DEMO – RouteConstraints and Routes

1
3

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

• Enumerate...
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...
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.N...
Helpful Links
THE MOST IMPORTANT LINK I COULD EVER TELL YOU:
http://aspnetwebstack.codeplex.com/
http://mvccontrib.codeple...
Please Leave Feedback During Q&A
If you leave session
feedback and
provide contact
information, you
will be qualified for
...
Upcoming SlideShare
Loading in …5
×

ASP.NET Routing Deep Dive

8,672 views

Published on

ASP.NET Routing gives us the ability to forge our own paths towards a more expressive search friendly web. In this presentation we’ll take a look at the ins and outs of ASP.NET Routing as it is used in both ASP.NET MVC and ASP.NET WebForms and how to customize your web routes to your tailored needs.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
8,672
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
29
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • 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.
  • 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.
  • ASP.NET Routing Deep Dive

    1. 1. ASP.NET ROUTING DEEP DIVE Stacy Vicknair Sparkhound 0
    2. 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. 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. 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. 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. 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. 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
    8. 8. HttpModules and HttpHandlers 7
    9. 9. ASP.NET Routing (System.Web.Routing) 8
    10. 10. ASP.NET MVC Routing (System.Web.Mvc) 9
    11. 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. 12. DEMO – Out of the box MVC Routing 1 1 11
    13. 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. 14. DEMO – RouteConstraints and Routes 1 3 13
    15. 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. 16. DEMO – Unit Testing with Routes 1 5 15
    17. 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. 18. DEMO – Routes in ASP.NET WebForms 1 7 17
    19. 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. 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. 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

    ×