Asp.net routing with mvc deep dive

15
ASP.NET Routing with MVC Deep Dive

description

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 ASP.NET MVC and how to customize your web routes to your tailored needs.

Transcript of Asp.net routing with mvc deep dive

Page 1: Asp.net routing with mvc deep dive

ASP.NET Routing with MVC Deep Dive

Page 2: Asp.net routing with mvc deep dive

2

About Me

Stacy Vicknair, MVP MCPDDevelopment Consultant for Sparkhound

Blog - http://www.wtfnext.comBRAG - http://www.braglunch.comTM - http://www.batonrougespeaks.com

Twitter: @svicknEmail: [email protected]

Page 3: Asp.net routing with mvc deep dive

3

Agenda

− What is routing? − Out of the box ASP.NET MVC routing

− Demo− Customizing ASP.NET MVC routing

− Demo: RouteConstraints and Routes− Routing concerns

Page 4: Asp.net routing with mvc deep dive

4

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

Page 5: Asp.net routing with mvc deep dive

5

HttpModules and HttpHandlers

Http Module 1

Http Module 2

Http Module 3

Http Handler

Http Handler

Http Handler

Page 6: Asp.net routing with mvc deep dive

6

ASP.NET Routing (System.Web.Routing)

UrlRoutingModuleIHttpModule

PageRouteHandlerIRouteHandler

UrlRoutingHandlerIHttpHandler

Content (Page, etc.)

Page 7: Asp.net routing with mvc deep dive

7

DefaultControllerFactoryIControllerFactory

ASP.NET MVC Routing (System.Web.Mvc)

UrlRoutingModuleIHttpModule

MvcRouteHandlerIRouteHandler

MvcHandlerUrlRoutingHandler

MyController.Execute()Controller

Page 8: Asp.net routing with mvc deep dive

8

Out of the box MVC Routing

− Routes.IgnoreRoute()− System.Web.Routing.StopRoutingHandler

− Routes.MapRoute()− System.Web.Mvc.RouteCollectionExtensions− Parameters

− 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"}

Page 9: Asp.net routing with mvc deep dive

9

DEMO – Out of the box MVC Routing

Page 10: Asp.net routing with mvc deep dive

10

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

Page 11: Asp.net routing with mvc deep dive

11

DEMO – RouteConstraints and Routes

Page 12: Asp.net routing with mvc deep dive

12

Routing Concerns

− IIS6 requires special attention− Add .mvc extension to aspnet_isapi.dll in ISAPI mapping− “{controller}.mvc/{action}/{id}”− Can use wildcard mapping, but performance is

questionable

Page 13: Asp.net routing with mvc deep dive

13

Agenda

− What is routing? − Out of the box ASP.NET MVC routing

− Demo− Customizing ASP.NET MVC routing

− Demo: RouteConstraints and Routes− Routing concerns

Page 14: Asp.net routing with mvc deep dive

14

Helpful Links

Google or Bing: ASP.NET Routing

http://www.15seconds.com/Issue/020417.htmhttp://msdn.microsoft.com/en-us/library/

cc668201.aspxhttp://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

Page 15: Asp.net routing with mvc deep dive

15

About Me

Stacy Vicknair, MVP MCPDDevelopment Consultant for Sparkhound

Blog - http://www.wtfnext.comBRAG - http://www.braglunch.comTM - http://www.batonrougespeaks.com

Twitter: @svicknEmail: [email protected]