Routing in
ASP.NET MVC
Creating a RESTful interface
to your site
Remember, browsers make
HTTP requests
HTTP requests use a verb to
communicate their intent
› GET
› POST
› PUT
› DELETE
Alright. Got that out of
the way. Now back to
our regularlyscheduled lecture.
›  asdf
Which is better?
This …
http://tic.com/Report.aspx?
t=Sales&y=2013&m=07!
… or this …
http://tic.com/Sales/2013/7!
Why?
Reasons
›  SEO
›  Easy

to type
›  Easy to remember
›  Hides implementation stack
›  URLs don’t have to change when t...
URLs point to resources.
›  Uniform

________________ locator
›  They don’t point to pages
›  So RESTful URLs are more ...
HI, I'm Roy
Fielding, the
inventor of REST.

REST
is ...

an architectural style …

for addressing resources …

in a state...
A RESTful URL is one that
conforms to all of the constraints
›  Separation

between client and server
›  Stateless commu...
A layered system has predictable
parts
The usual method

Properly RESTful url

Category

Category

Thing

Thing

Action

I...
Use the HTTP requests to specify
the action
› GET
› POST
› PUT
› DELETE
The URL should act as an API
› http://tic.com/Customer/List
› http://tic.com/Customer/Create
› http://tic.com/Customer/...
Or better yet ...
› GET

http://tic.com/Customer
› POST http://tic.com/Customer
› GET http://tic.com/Customer/71
› PUT...
There are certain things you
can do to make a URL RESTful
›  Give

every “thing” an ID
›  Link things together
›  Use s...
Resources with multiple representations
› HTML
› XML
› VCard
› RSS
Okay, I'm sold on RESTful urls.
How do I get them into
MVC?
Registering
routes tells
IIS that if a
user asks
for X, send
them to Y
We use the MapRoute()
method to create these routes
MyRoutes.MapRoute(!
string RouteName,!
string UrlPattern,!
object Defa...
Routing in ASP.NET MVC are
set in Application_Start
›  Pulled

out into a static method by default.

public static void R...
Routes are matched in order
that they appear
Hardcoded routes are easy
routes.MapRoute(!
"ListAllProducts",!
"Product/List",!
new { !
controller="Home", !
action="Show...
Hands-on literal routes
Can receive arguments by
putting them in curly braces
›  aka.

URL Parameters
Examples
Route definition

Example of matching url

{controller}/{action}/{category}

Product/show/beverage

{table}/detai...
Hands-on matching routes
Optional parameters can save you
from creating a lot of routes
›  Example:

you want to match

›  Clients/Miller
›  Cli...
Hands-on optional parameters
Hands-on default values
Regular expression constraints
routes.MapRoute(!
"BlogArchive",!
"Archive/{entryDate}",!
new { controller="Blog", action="...
Hands-on constraints
Conclusion
›  RESTful

urls are easier to use, therefore better
›  MVC lends itself well to REST; in fact REST is pretty...
Oh! And one more thing ...
These routes work for WebForms
also
›  They're

exactly the same.
Upcoming SlideShare
Loading in...5
×

ASP.NET MVC routing

3,266

Published on

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

  • Be the first to like this

No Downloads
Views
Total Views
3,266
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
21
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

ASP.NET MVC routing

  1. 1. Routing in ASP.NET MVC Creating a RESTful interface to your site
  2. 2. Remember, browsers make HTTP requests
  3. 3. HTTP requests use a verb to communicate their intent › GET › POST › PUT › DELETE
  4. 4. Alright. Got that out of the way. Now back to our regularlyscheduled lecture. ›  asdf
  5. 5. Which is better? This … http://tic.com/Report.aspx? t=Sales&y=2013&m=07! … or this … http://tic.com/Sales/2013/7! Why?
  6. 6. Reasons ›  SEO ›  Easy to type ›  Easy to remember ›  Hides implementation stack ›  URLs don’t have to change when the site changes. ›  Hackable
  7. 7. URLs point to resources. ›  Uniform ________________ locator ›  They don’t point to pages ›  So RESTful URLs are more accurate, actually
  8. 8. HI, I'm Roy Fielding, the inventor of REST. REST is ... an architectural style … for addressing resources … in a stateless environment … that reduces coupling
  9. 9. 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
  10. 10. A layered system has predictable parts The usual method Properly RESTful url Category Category Thing Thing Action ID ID (Use HTTP method as action)
  11. 11. Use the HTTP requests to specify the action › GET › POST › PUT › DELETE
  12. 12. The URL should act as an API › http://tic.com/Customer/List › http://tic.com/Customer/Create › http://tic.com/Customer/Details/71 › http://tic.com/Customer/Edit/71 › http://tic.com/Customer/Delete/71 › http://tic.com/Department › http://tic.com/Employee › http://tic.com/AddToCart/17
  13. 13. Or better yet ... › GET http://tic.com/Customer › POST http://tic.com/Customer › GET http://tic.com/Customer/71 › PUT http://tic.com/Customer/71 › DELETE http://tic.com/Customer/71 › GET http://tic.com/Department › GET http://tic.com/Employee › POST http://tic.com/AddToCart/17
  14. 14. There are certain things you can do to make a URL RESTful ›  Give every “thing” an ID ›  Link things together ›  Use standard methods ›  Communicate statelessly ›  Provide multiple representations
  15. 15. Resources with multiple representations › HTML › XML › VCard › RSS
  16. 16. Okay, I'm sold on RESTful urls. How do I get them into MVC?
  17. 17. Registering routes tells IIS that if a user asks for X, send them to Y
  18. 18. We use the MapRoute() method to create these routes MyRoutes.MapRoute(! string RouteName,! string UrlPattern,! object Defaults);! ›  Says when an url matching UrlPattern comes through, what do we do with it?
  19. 19. 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! );! }!
  20. 20. Routes are matched in order that they appear
  21. 21. Hardcoded routes are easy routes.MapRoute(! "ListAllProducts",! "Product/List",! new { ! controller="Home", ! action="ShowProducts"! }! );!
  22. 22. Hands-on literal routes
  23. 23. Can receive arguments by putting them in curly braces ›  aka. URL Parameters
  24. 24. Examples Route definition Example of matching url {controller}/{action}/{category} Product/show/beverage {table}/details.aspx Products/Details.aspx blog/{action}/{entry} blog/read/1234 blog/{entry}-{SEOString} blog/2234-Why-string-theory-isflawed {reportType}/{year}/{month}/{day} sales/2012/7/15 {locale}/{action} en-US/show
  25. 25. Hands-on matching routes
  26. 26. 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 }!
  27. 27. Hands-on optional parameters
  28. 28. Hands-on default values
  29. 29. Regular expression constraints routes.MapRoute(! "BlogArchive",! "Archive/{entryDate}",! new { controller="Blog", action="Archive" }, ! new { entryDate=@"d{2}-d{2}-d{4}" }! );!
  30. 30. Hands-on constraints
  31. 31. 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 using RegisterRoutes()
  32. 32. Oh! And one more thing ... These routes work for WebForms also ›  They're exactly the same.
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×