adding a few tests for areas

This commit is contained in:
Ryan Nowak 2014-07-21 14:50:04 -07:00
parent 1b07c89322
commit d13f6474d8
3 changed files with 117 additions and 1 deletions

View File

@ -272,6 +272,18 @@ namespace Microsoft.AspNet.Mvc
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);
}
}
}
}

View File

@ -171,7 +171,15 @@ namespace Microsoft.AspNet.Mvc.Routing
object 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);

View File

@ -296,6 +296,102 @@ namespace Microsoft.AspNet.Mvc.Routing
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)
{
var httpContext = Mock.Of<HttpContext>();