14. Defaults
Or Defaults: Variable Value
“product/index” controller Product
? action Index
routes.MapRoute( id
Id !
3
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { id=3 }
);
15. UrlParameter.Optional
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}", Variable value
defaults: new {action = "Index“, id=UrlParameter.Optinal} controller Product
); action Index
id 3
• Supplied: “product/index/3”
• Not supplied: Variable value
“product/index” controller Product
action index
16. No excessive number of segments
“,controller-/,action-/,id-”
“products/oranges/edit/3”
Unless:
“,controller}/{action}/{id}/{*catchall-”
“product/oranges/edit/3/something’
Variable value
controller Product
action Index
id edit
Catchall 3/something
17. Constraints
1. Regular Expressions:
routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", …
constraints: new { controller = "^H.*" } );
2. Specific values:
constraints: new { action = "^Index$|^About$*" }
3. HTTP methods:
constraints: new { httpMethod= new HttpMethodConstraint("GET") }
4. Custom constraints:
bool IRouteConstraint.Match(HttpContextBase, Route, stringParameterName, RouteValueDictionary,
RouteDireciont)
21. Helpers
1. Html.ActionLink(text, nameOfAction, nameOfController, RouteValues, HtmlAttributes)
2. Url.Action(text, nameOfAction, nameOfController, RouteValues)
3. @Html.RouteLink(text, RouteValues, HtmlAttributes)
4. Url.RouteLink(text, AnonymouseTypeValues)
• You can use Route name
22. Rules
1. All variables should have values: “,controller-/,action-/,id-”
a) Supplied
b) Current request
c) Default values
2. Should not dis-agree with default-only variables:
routes.MapRoute("MyRoute", "{controller}/{action}", new { myVar = "true" });
3. Satisfy constraints
23. Notes (Outgoing)
• Tricky: Reuse values of the current request URL:
url: "{controller}/{action}/{id}/{forth}",
defaults: new { controller = "Home", action = "Index", id = 3, forth=8},
------------
@Html.ActionLink("this is the link", "Index", new {id=5 });
@Html.ActionLink("this is the link", "Index", new {forth=8 });
• Url’s generated will try to produce the shortest url
25. Areas
public class AdminAreaRegistration : AreaRegistration {
public override string AreaName { get {
return "Admin"; } }
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
28. Unit-Testing Routes (Incoming)
What do we want to test?
That the url patterns we want to support in our web app would populate the RouteData variables
as expected.
32. Design Guidelines
• Content vs Implementation
• Human friendly: content titles vs id’s
• Hackable
• Sense of hierarchy
• Static strings at the beggning of routes
• Dash instead of underscore
• Not too long
• Avoid complexity
• Be consistent
33. ASP.NET
It’s all open source!
http://aspnetwebstack.codeplex.com/
Some historyA page is an HttpHandlerToo rigidBound to a fileThis will not work for MVC, they need to invoke Methods (Actions) in Classes (Controller)So they wanted more flexibilityFile/extension agnosticDifferent handlers (especially they needed to introduce the separation of concerns and invoking controllers on certain points “actions”)
When a request comes to the server, how can the app know how to handle this request?Emphasize on RouteData being passed to the handler
So we could be rigid, or dynamicBut how can we make programmers more interesting and harder? :P
At the startup of the web app
It’s hard to put it in steps since it’s little bit complex and wined together, but we will try
“To be clear, it is not that the value of id is null when nocorresponding segment is supplied; rather, the case is that an id variable is not defined”To distinguish if user sent a value or notSeparation of concerns (defaults in routing?)
Specific values for controllers that might share a URL patternHttp Method has nothing to do with the “post” and “get” action filters
Inspired by RouteDebugger created by Phil Haack
Last segment includes the query string, but it’s up to the IHttpHandler how to handle it
Don’t use fixed URL’s!Routing doesn’t understand what “controller” is or what “action” is.Explain parametersExtra values are added as aquery stringThe Html.Action method uses routing as well, it’s not a direct call
Reuse values of the current request URL only for segment variables that occur earlier in the URL pattern than any parameters that are supplied to the Html.ActionLink method:
Don’t change namespace of controller in an AreaDo use namespace priority for the general controller
Don’t change the order of Area registration to be after Routing
Because it’s the key for the MVCHttpHandlerBy: checking the orderChecking the static stringsChecking constraints if we have them
Before, it was hard to test Routes, now they provided HttpContextBase
Before, it was hard to test Routes, now they provided HttpContextBase
“offer” might be date as wellI am not totally enthusiast since this is not testing Routing only, it intervenes of how binding works
I don’t like best practicesIf external website maybe you want to follow the SEOAvoid complexityContent vs Implementation: REST or Action? if easy go RESTful, no just don’t not too longContent title’s vs ID’s: avoid ID’s in general