10 routing
-
Upload
rap-payne -
Category
Technology
-
view
299 -
download
0
Transcript of 10 routing
![Page 1: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/1.jpg)
Routing in ASP.NET MVC
![Page 2: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/2.jpg)
Remember, browsers make HTTP requests
![Page 3: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/3.jpg)
HTTP requests use a verb to communicate their intent
� GET � POST � PUT � DELETE
![Page 4: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/4.jpg)
� asdf
Alright. Got that out of the way. Now back to our regularly-scheduled lecture.
![Page 5: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/5.jpg)
Which is better? This …
http://www.tic.com/Report.aspx?
Type=Sales&year=2012&month=07!
… or this …
http://www.tic.com/Sales/2012/7!
Why?
![Page 6: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/6.jpg)
Reasons � SEO � Easy to type � Easy to remember � Hides implementation stack � URLs don’t have to change when the site
changes. � Hackable
![Page 7: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/7.jpg)
URLs point to resources. � Uniform ________________ locator � They don’t point to pages � So RESTful URLs are more accurate,
actually
![Page 8: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/8.jpg)
REST is … � an architectural style … � for addressing resources … � in a stateless environment … � that reduces coupling
![Page 9: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/9.jpg)
A RESTful URL is one that conforms to all of the constraints � Separation between client and server � Stateless communication � Cacheable � A layered system � A uniform interface
![Page 10: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/10.jpg)
A layered system has predictable parts
The usual method
Category
Thing
Action
ID
Properly RESTful url
Category
Thing
ID
(Use HTTP method as action)
![Page 11: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/11.jpg)
Use the HTTP requests to specify the action
� GET � POST � PUT � DELETE
![Page 12: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/12.jpg)
The URL should act as an API
� http://tic.com/Associate/List � http://tic.com/Associate/Create � http://tic.com/Associate/Details/71 � http://tic.com/Associate/Edit/71 � http://tic.com/Associate/Delete/71 � http://tic.com/Department � http://tic.com/Customer � http://tic.com/AddToCart/17
![Page 13: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/13.jpg)
There are certain things you can do to make a URL RESTful � Give every “thing” an ID � Link things together � Use standard methods � Resources with multiple representations � Communicate statelessly
![Page 14: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/14.jpg)
Give every thing an ID
� Easy to do if you're using a primary key and a database behind it
![Page 15: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/15.jpg)
Link things together
� Also easy to do if you're using HTML
![Page 16: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/16.jpg)
Use standard methods
![Page 17: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/17.jpg)
Resources with multiple representations
� HTML � XML � VCard
![Page 18: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/18.jpg)
Communicate statelessly � Aaaand yet again, the web makes this easy.
![Page 19: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/19.jpg)
Okay, I'm sold on RESTful urls. How do I get them into MVC?
![Page 20: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/20.jpg)
Registering routes tells IIS that if a user asks for X, send them to Y
![Page 21: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/21.jpg)
We use the MapRoute() method to create these routes MyRoutes.MapRoute(! string RouteName,! string URL,! object Defaults);!� Says when URL comes through, what do
we do with it?
![Page 22: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/22.jpg)
Routing in ASP.NET MVC are set 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! );!}!
![Page 23: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/23.jpg)
Routes are matched in order that they appear
![Page 24: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/24.jpg)
These routes work for WebForms also � They're exactly the same.
![Page 25: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/25.jpg)
Hardcoded routes are easy routes.MapRoute(! "ListAllProducts",! "Product/List",! "~/Admin/Products.aspx?id=all",! false! );!
![Page 26: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/26.jpg)
Hands-on literal routes
![Page 27: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/27.jpg)
We put placeholders in curly braces � aka. URL Parameters
![Page 28: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/28.jpg)
Examples Route definition Example of matching url
{controller}/{action}/{category}
/Products/show/beverage
{table}/details.aspx /Products/Details.aspx
blog/{action}/{entry} blog/read/1234
{reportType}/{year}/{month}/{day}
/sales/2012/7/15
{locale}/{action} en-US/show
![Page 29: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/29.jpg)
Hands-on matching routes
![Page 30: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/30.jpg)
Optional parameters can save you from creating a lot of routes � 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 }
![Page 31: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/31.jpg)
Hands-on optional parameters
![Page 32: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/32.jpg)
Hands-on default values
![Page 33: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/33.jpg)
The parameters can have one of two types of constraints 1. Regular expressions 2. An IRouteConstraint
![Page 34: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/34.jpg)
Regular expression constraints routes.MapRoute(! "BlogArchive",! "Archive/{entryDate}",! new { controller="Blog", action="Archive" }, ! new { entryDate=@"\d{2}-\d{2}-\d{4}" }!);!
![Page 35: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/35.jpg)
Hands-on constraints
![Page 36: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/36.jpg)
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
![Page 37: 10 routing](https://reader034.fdocuments.net/reader034/viewer/2022052410/5555d3e6d8b42aaf158b4e63/html5/thumbnails/37.jpg)
Further study � Intro to REST
� http://www.infoq.com/articles/rest-introduction