Adding object-dictionary support for defaults
This commit is contained in:
parent
4022e5a5a4
commit
bdd4513b88
|
|
@ -28,7 +28,7 @@ namespace RoutingSample
|
|||
|
||||
var rb1 = new RouteBuilder(endpoint1, routes);
|
||||
rb1.AddPrefixRoute("api/store");
|
||||
rb1.AddTemplateRoute("api/checkout/{*extra}");
|
||||
rb1.AddTemplateRoute("api/{controller}/{*extra}", new { controller = "Store" });
|
||||
|
||||
var rb2 = new RouteBuilder(endpoint2, routes);
|
||||
rb2.AddPrefixRoute("hello/world");
|
||||
|
|
|
|||
|
|
@ -0,0 +1,33 @@
|
|||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Microsoft.AspNet.Routing
|
||||
{
|
||||
public class RouteValueDictionary : Dictionary<string, object>
|
||||
{
|
||||
public RouteValueDictionary()
|
||||
: base(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
}
|
||||
|
||||
public RouteValueDictionary(object obj)
|
||||
: base(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
if (obj != null)
|
||||
{
|
||||
foreach (var property in obj.GetType().GetTypeInfo().DeclaredProperties)
|
||||
{
|
||||
var value = property.GetValue(obj);
|
||||
Add(property.Name, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public RouteValueDictionary(IDictionary<string, object> other)
|
||||
: base(other, StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -15,5 +15,10 @@ namespace Microsoft.AspNet.Routing.Template
|
|||
{
|
||||
builder.Routes.Add(new TemplateRoute(builder.Endpoint, template, defaults));
|
||||
}
|
||||
|
||||
public static void AddTemplateRoute(this IRouteBuilder builder, string template, object defaults)
|
||||
{
|
||||
builder.Routes.Add(new TemplateRoute(builder.Endpoint, template, new RouteValueDictionary(defaults)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue