Add a sample of building a customize 'MapRoute' method

This is a sample of the pattern for building a MapRoute method that
customizes routing by adding constraints, datatokens, or defaults. This is
the replacement for adding data to the route directly.

Also fixed up the sample to work. It was massively out of date.
This commit is contained in:
Ryan Nowak 2014-11-18 11:58:29 -08:00
parent 01345eca91
commit 5c116db6a5
3 changed files with 51 additions and 12 deletions

View File

@ -1,5 +1,10 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNet.Routing;
using Microsoft.AspNet.Routing.Template;
using Microsoft.Framework.DependencyInjection;
namespace RoutingSample.Web
{
@ -28,5 +33,28 @@ namespace RoutingSample.Web
routeBuilder.Routes.Add(new PrefixRoute(handler, prefix));
return routeBuilder;
}
public static IRouteBuilder MapLocaleRoute(
this IRouteBuilder routeBuilder,
string locale,
string routeTemplate,
object defaults)
{
var defaultsDictionary = new RouteValueDictionary(defaults);
defaultsDictionary.Add("locale", locale);
var constraintResolver = routeBuilder.ServiceProvider.GetService<IInlineConstraintResolver>();
var route = new TemplateRoute(
target: routeBuilder.DefaultHandler,
routeTemplate: routeTemplate,
defaults: defaultsDictionary,
constraints: null,
dataTokens: null,
inlineConstraintResolver: constraintResolver);
routeBuilder.Routes.Add(route);
return routeBuilder;
}
}
}

View File

@ -1,12 +1,11 @@
using System;
using System.Collections.Generic;
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Text.RegularExpressions;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Routing;
using Microsoft.AspNet.Routing.Constraints;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.OptionsModel;
namespace RoutingSample.Web
{
@ -56,7 +55,12 @@ namespace RoutingSample.Web
new { controller = "Store" });
routeBuilder.AddPrefixRoute("hello/world", endpoint2);
routeBuilder.MapLocaleRoute("en-US", "store/US/{action}", new { controller = "Store" });
routeBuilder.MapLocaleRoute("en-GB", "store/UK/{action}", new { controller = "Store" });
routeBuilder.AddPrefixRoute("", endpoint2);
builder.UseRouter(routeBuilder.Build());
}
}

View File

@ -1,10 +1,17 @@
{
"dependencies": {
"Microsoft.AspNet.Server.IIS": "1.0.0-*",
"Microsoft.AspNet.Routing" : "1.0.0-*"
},
"frameworks": {
"aspnet50": {},
"aspnetcore50": {}
}
"dependencies": {
"Kestrel": "1.0.0-*",
"Microsoft.AspNet.Server.IIS": "1.0.0-*",
"Microsoft.AspNet.Server.WebListener": "1.0.0-*",
"Microsoft.AspNet.Routing": "1.0.0-*"
},
"commands": {
"web": "Microsoft.AspNet.Hosting server=Microsoft.AspNet.Server.WebListener server.urls=http://localhost:5001",
"kestrel": "Microsoft.AspNet.Hosting --server Kestrel --server.urls http://localhost:5000"
},
"frameworks": {
"aspnet50": { },
"aspnetcore50": { }
},
"webroot": "wwwroot"
}