ASP.NET MVC routing

32
Routing in ASP.NET MVC Creating a RESTful interface to your site

description

 

Transcript of ASP.NET MVC routing

Page 1: ASP.NET MVC routing

Routing in ASP.NET MVC Creating a RESTful interface to your site

Page 2: ASP.NET MVC routing

Remember, browsers make HTTP requests

Page 3: ASP.NET MVC routing

HTTP requests use a verb to communicate their intent

� GET � POST � PUT � DELETE

Page 4: ASP.NET MVC routing

� asdf

Alright. Got that out of the way. Now back to our regularly-scheduled lecture.

Page 5: ASP.NET MVC routing

Which is better? This …

http://tic.com/Report.aspx?

t=Sales&y=2013&m=07!

… or this …

http://tic.com/Sales/2013/7!

Why?

Page 6: ASP.NET MVC routing

Reasons � SEO � Easy to type � Easy to remember � Hides implementation stack � URLs don’t have to change when the site

changes. � Hackable

Page 7: ASP.NET MVC routing

URLs point to resources. � Uniform ________________ locator � They don’t point to pages � So RESTful URLs are more accurate,

actually

Page 8: ASP.NET MVC routing

that reduces coupling

in a stateless environment …

an architectural style … REST is ...

HI, I'm Roy Fielding, the

inventor of REST.

for addressing resources …

Page 9: ASP.NET MVC routing

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: ASP.NET MVC routing

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: ASP.NET MVC routing

Use the HTTP requests to specify the action

� GET � POST � PUT � DELETE

Page 12: ASP.NET MVC routing

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

Page 13: ASP.NET MVC routing

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

Page 14: ASP.NET MVC routing

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

Page 15: ASP.NET MVC routing

Resources with multiple representations

� HTML � XML � VCard � RSS

Page 16: ASP.NET MVC routing

Okay, I'm sold on RESTful urls. How do I get them into MVC?

Page 17: ASP.NET MVC routing

Registering routes tells IIS that if a user asks for X, send them to Y

Page 18: ASP.NET MVC routing

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?

Page 19: ASP.NET MVC routing

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 20: ASP.NET MVC routing

Routes are matched in order that they appear

Page 21: ASP.NET MVC routing

Hardcoded routes are easy routes.MapRoute(! "ListAllProducts",! "Product/List",! new { ! controller="Home", ! action="ShowProducts"! }!);!

Page 22: ASP.NET MVC routing

Hands-on literal routes

Page 23: ASP.NET MVC routing

Can receive arguments by putting them in curly braces � aka. URL Parameters

Page 24: ASP.NET MVC routing

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-is-flawed

{reportType}/{year}/{month}/{day} sales/2012/7/15

{locale}/{action} en-US/show

Page 25: ASP.NET MVC routing

Hands-on matching routes

Page 26: ASP.NET MVC routing

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 27: ASP.NET MVC routing

Hands-on optional parameters

Page 28: ASP.NET MVC routing

Hands-on default values

Page 29: ASP.NET MVC routing

Regular expression constraints routes.MapRoute(! "BlogArchive",! "Archive/{entryDate}",! new { controller="Blog", action="Archive" }, ! new { entryDate=@"\d{2}-\d{2}-\d{4}" }!);!

Page 30: ASP.NET MVC routing

Hands-on constraints

Page 31: ASP.NET MVC routing

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()

Page 32: ASP.NET MVC routing

Oh! And one more thing ... These routes work for WebForms also � They're exactly the same.