adding a few tests for areas
This commit is contained in:
parent
1b07c89322
commit
d13f6474d8
|
|
@ -272,6 +272,18 @@ namespace Microsoft.AspNet.Mvc
|
||||||
RouteKeyHandling.DenyKey));
|
RouteKeyHandling.DenyKey));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// We still want to add a 'null' for any constraint with DenyKey so that link generation
|
||||||
|
// works properly.
|
||||||
|
//
|
||||||
|
// Consider an action like { area = "", controller = "Home", action = "Index" }. Even if
|
||||||
|
// it's attribute routed, it needs to know that area must be null to generate a link.
|
||||||
|
if (!actionDescriptor.RouteValueDefaults.ContainsKey(key))
|
||||||
|
{
|
||||||
|
actionDescriptor.RouteValueDefaults.Add(key, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -171,7 +171,15 @@ namespace Microsoft.AspNet.Mvc.Routing
|
||||||
object providedValue;
|
object providedValue;
|
||||||
if (!context.Values.TryGetValue(key, out providedValue))
|
if (!context.Values.TryGetValue(key, out providedValue))
|
||||||
{
|
{
|
||||||
context.AmbientValues.TryGetValue(key, out providedValue);
|
// If the required value is an 'empty' route value, then ignore ambient values.
|
||||||
|
// This handles a case where we're generating a link to an action like:
|
||||||
|
// { area = "", controller = "Home", action = "Index" }
|
||||||
|
//
|
||||||
|
// and the ambient values has a value for area.
|
||||||
|
if (value != null)
|
||||||
|
{
|
||||||
|
context.AmbientValues.TryGetValue(key, out providedValue);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return TemplateBinder.RoutePartsEqual(providedValue, value);
|
return TemplateBinder.RoutePartsEqual(providedValue, value);
|
||||||
|
|
|
||||||
|
|
@ -296,6 +296,102 @@ namespace Microsoft.AspNet.Mvc.Routing
|
||||||
Assert.Equal(2, callCount);
|
Assert.Equal(2, callCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void AttributeRoute_GenerateLink_ToArea()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var entry1 = CreateGenerationEntry("Help/Store", new { area = "Help", action = "Edit", controller = "Store" });
|
||||||
|
entry1.Precedence = 1;
|
||||||
|
|
||||||
|
var entry2 = CreateGenerationEntry("Store", new { area = (string)null, action = "Edit", controller = "Store" });
|
||||||
|
entry2.Precedence = 2;
|
||||||
|
|
||||||
|
var next = new StubRouter();
|
||||||
|
|
||||||
|
var route = CreateAttributeRoute(next, entry1, entry2);
|
||||||
|
|
||||||
|
var context = CreateVirtualPathContext(new { area = "Help", action = "Edit", controller = "Store" });
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var path = route.GetVirtualPath(context);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal("Help/Store", path);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void AttributeRoute_GenerateLink_ToArea_PredecedenceReversed()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var entry1 = CreateGenerationEntry("Help/Store", new { area = "Help", action = "Edit", controller = "Store" });
|
||||||
|
entry1.Precedence = 2;
|
||||||
|
|
||||||
|
var entry2 = CreateGenerationEntry("Store", new { area = (string)null, action = "Edit", controller = "Store" });
|
||||||
|
entry2.Precedence = 1;
|
||||||
|
|
||||||
|
var next = new StubRouter();
|
||||||
|
|
||||||
|
var route = CreateAttributeRoute(next, entry1, entry2);
|
||||||
|
|
||||||
|
var context = CreateVirtualPathContext(new { area = "Help", action = "Edit", controller = "Store" });
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var path = route.GetVirtualPath(context);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal("Help/Store", path);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void AttributeRoute_GenerateLink_ToArea_WithAmbientValues()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var entry1 = CreateGenerationEntry("Help/Store", new { area = "Help", action = "Edit", controller = "Store" });
|
||||||
|
entry1.Precedence = 1;
|
||||||
|
|
||||||
|
var entry2 = CreateGenerationEntry("Store", new { area = (string)null, action = "Edit", controller = "Store" });
|
||||||
|
entry2.Precedence = 2;
|
||||||
|
|
||||||
|
var next = new StubRouter();
|
||||||
|
|
||||||
|
var route = CreateAttributeRoute(next, entry1, entry2);
|
||||||
|
|
||||||
|
var context = CreateVirtualPathContext(
|
||||||
|
values: new { action = "Edit", controller = "Store" },
|
||||||
|
ambientValues: new { area = "Help" });
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var path = route.GetVirtualPath(context);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal("Help/Store", path);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void AttributeRoute_GenerateLink_OutOfArea_IgnoresAmbientValue()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var entry1 = CreateGenerationEntry("Help/Store", new { area = "Help", action = "Edit", controller = "Store" });
|
||||||
|
entry1.Precedence = 1;
|
||||||
|
|
||||||
|
var entry2 = CreateGenerationEntry("Store", new { area = (string)null, action = "Edit", controller = "Store" });
|
||||||
|
entry2.Precedence = 2;
|
||||||
|
|
||||||
|
var next = new StubRouter();
|
||||||
|
|
||||||
|
var route = CreateAttributeRoute(next, entry1, entry2);
|
||||||
|
|
||||||
|
var context = CreateVirtualPathContext(
|
||||||
|
values: new { action = "Edit", controller = "Store" },
|
||||||
|
ambientValues: new { area = "Blog" });
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var path = route.GetVirtualPath(context);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal("Store", path);
|
||||||
|
}
|
||||||
|
|
||||||
private static VirtualPathContext CreateVirtualPathContext(object values, object ambientValues = null)
|
private static VirtualPathContext CreateVirtualPathContext(object values, object ambientValues = null)
|
||||||
{
|
{
|
||||||
var httpContext = Mock.Of<HttpContext>();
|
var httpContext = Mock.Of<HttpContext>();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue