Your SlideShare is downloading. ×
10 routing
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

10 routing


Published on

Published in: Technology, Design
  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Routing inASP.NET MVC
  • 2. Remember, browsers makeHTTP requests
  • 3. HTTP requests use a verb tocommunicate their intent› GET› POST› PUT› DELETE
  • 4. Alright. Got that out of the way. Now back to our regularly- scheduled lecture.›  asdf
  • 5. Which is better?This …!… or this …! Why?
  • 6. Reasons›  SEO›  Easy to type›  Easy to remember›  Hides implementation stack›  URLs don’t have to change when the site changes.›  Hackable
  • 7. URLs point to resources.›  Uniform ________________ locator›  They don’t point to pages›  So RESTful URLs are more accurate, actually
  • 8. REST is …›  an architectural style …›  for addressing resources …›  in a stateless environment …›  that reduces coupling
  • 9. A RESTful URL is one thatconforms to all of the constraints›  Separation between client and server›  Stateless communication›  Cacheable›  A layered system›  A uniform interface
  • 10. A layered system has predictableparts The usual method Properly RESTful url Category Category Thing Thing Action ID ID (Use HTTP method as action)
  • 11. Use the HTTP requests to specifythe action› GET› POST› PUT› DELETE
  • 12. The URL should act as an API››››››››
  • 13. There are certain things youcan do to make a URL RESTful›  Give every “thing” an ID›  Link things together›  Use standard methods›  Resources with multiple representations›  Communicate statelessly
  • 14. Give every thing an ID ›  Easy to do if youre using a primary key and a database behind it
  • 15. Link things together›  Also easy to do if youre using HTML
  • 16. Use standard methods
  • 17. Resources with multiple representations› HTML› XML› VCard
  • 18. Communicate statelessly›  Aaaand yet again, the web makes this easy.
  • 19. Okay, Im sold on RESTful urls.How do I get them intoMVC?
  • 20. Registeringroutes tellsIIS that if auser asksfor X, sendthem to Y
  • 21. We use the MapRoute()method to create these routesMyRoutes.MapRoute(! string RouteName,! string URL,! object Defaults);!›  Says when URL comes through, what do we do with it?
  • 22. Routing in ASP.NET MVC areset in Application_Start›  Pulled out into a static method by default.public static void RegisterRoutes(RouteCollection routes)!{! routes.IgnoreRoute("{resource}.axd/{*pathInfo}");! routes.MapRoute(! "Default", // Route name! "{controller}/{action}/{id}", // URL with parms! new { controller = "Home", action = "Index", ! id = UrlParameter.Optional } // defaults! );!}!
  • 23. Routes are matched in orderthat they appear
  • 24. These routes work for WebFormsalso›  Theyre exactly the same.
  • 25. Hardcoded routes are easyroutes.MapRoute(! "ListAllProducts",! "Product/List",! "~/Admin/Products.aspx?id=all",! false! );!
  • 26. Hands-on literal routes
  • 27. We put placeholders in curlybraces›  aka. URL Parameters
  • 28. ExamplesRoute definition Example of matching url{controller}/{action}/ /Products/show/beverage{category}{table}/details.aspx /Products/Details.aspxblog/{action}/{entry} blog/read/1234{reportType}/{year}/{month}/ /sales/2012/7/15{day}{locale}/{action} en-US/show
  • 29. Hands-on matching routes
  • 30. Optional parameters can saveyou from creating a lot ofroutes›  Example: you want to match›  Clients/Miller›  Clients/Miller/Harold›  Clients/Miller/Harold/Dr›  … with the same route›  Simply make first name and title optional with:›  new { FirstName = UrlParameter.Optional, Title = UrlParameter.Optional }
  • 31. Hands-on optional parameters
  • 32. Hands-on default values
  • 33. The parameters can have oneof two types of constraints1.  Regular expressions2.  An IRouteConstraint
  • 34. Regular expression constraintsroutes.MapRoute(! "BlogArchive",! "Archive/{entryDate}",! new { controller="Blog", action="Archive" }, ! new { entryDate=@"d{2}-d{2}-d{4}" }!);!
  • 35. Hands-on constraints
  • 36. Conclusion›  RESTful urls are easier to use, therefore better›  MVC lends itself well to REST; in fact REST is pretty much required to get to controllers and actions›  Routes are set up in the global.asax file
  • 37. Further study›  Intro to REST › introduction