/// <summary> /// Combines two <see cref="AttributeRouteModel"/> instances and returns /// a new <see cref="AttributeRouteModel"/> instance with the result. /// </summary> /// <param name="left">The left <see cref="AttributeRouteModel"/>.</param> /// <param name="right">The right <see cref="AttributeRouteModel"/>.</param> /// <returns>A new instance of <see cref="AttributeRouteModel"/> that represents the /// combination of the two <see cref="AttributeRouteModel"/> instances or <c>null</c> if both /// parameters are <c>null</c>.</returns> public static AttributeRouteModel CombineAttributeRouteModel( AttributeRouteModel left, AttributeRouteModel right) { right = right ?? _default; // If the right template is an override template (starts with / or ~/) // we ignore the values from left. if (left == null || IsOverridePattern(right.Template)) { left = _default; } var combinedTemplate = CombineTemplates(left.Template, right.Template); // The action is not attribute routed. if (combinedTemplate == null) { return null; } return new AttributeRouteModel() { Template = combinedTemplate, Order = right.Order ?? left.Order, Name = ChooseName(left, right), }; }
public void CopyConstructor_DoesDeepCopyOfOtherModels() { // Arrange var controller = new ControllerModel(typeof(TestController).GetTypeInfo(), new List<object>()); var action = new ActionModel(typeof(TestController).GetMethod("Edit"), new List<object>()); controller.Actions.Add(action); action.Controller = controller; var route = new AttributeRouteModel(new HttpGetAttribute("api/Products")); controller.AttributeRoutes.Add(route); var apiExplorer = controller.ApiExplorer; controller.ApiExplorer.GroupName = "group"; controller.ApiExplorer.IsVisible = true; // Act var controller2 = new ControllerModel(controller); // Assert Assert.NotSame(action, controller2.Actions[0]); Assert.NotSame(route, controller2.AttributeRoutes[0]); Assert.NotSame(apiExplorer, controller2.ApiExplorer); Assert.NotSame(controller.ActionConstraints, controller2.ActionConstraints); Assert.NotSame(controller.Actions, controller2.Actions); Assert.NotSame(controller.Attributes, controller2.Attributes); Assert.NotSame(controller.Filters, controller2.Filters); Assert.NotSame(controller.RouteConstraints, controller2.RouteConstraints); }
public void CopyConstructor_DoesDeepCopyOfOtherModels() { // Arrange var action = new ActionModel(typeof(TestController).GetMethod("Edit"), new List<object>()); var parameter = new ParameterModel(action.ActionMethod.GetParameters()[0], new List<object>()); parameter.Action = action; action.Parameters.Add(parameter); var route = new AttributeRouteModel(new HttpGetAttribute("api/Products")); action.AttributeRouteModel = route; var apiExplorer = action.ApiExplorer; apiExplorer.IsVisible = false; apiExplorer.GroupName = "group1"; // Act var action2 = new ActionModel(action); // Assert Assert.NotSame(action, action2.Parameters[0]); Assert.NotSame(apiExplorer, action2.ApiExplorer); Assert.NotSame(route, action2.AttributeRouteModel); }
public RouteConvention(string preffix = null) { _centralPrefix = new AttributeRouteModel(new RouteAttribute( (string.IsNullOrEmpty(preffix) == false ? preffix + "/" : "") + "[controller]")); }
public ActionModel(ActionModel other) { if (other == null) { throw new ArgumentNullException(nameof(other)); } ActionMethod = other.ActionMethod; ActionName = other.ActionName; // Not making a deep copy of the controller, this action still belongs to the same controller. Controller = other.Controller; // These are just metadata, safe to create new collections ActionConstraints = new List<IActionConstraintMetadata>(other.ActionConstraints); Attributes = new List<object>(other.Attributes); Filters = new List<IFilterMetadata>(other.Filters); HttpMethods = new List<string>(other.HttpMethods); Properties = new Dictionary<object, object>(other.Properties); // Make a deep copy of other 'model' types. ApiExplorer = new ApiExplorerModel(other.ApiExplorer); Parameters = new List<ParameterModel>(other.Parameters.Select(p => new ParameterModel(p))); RouteConstraints = new List<IRouteConstraintProvider>(other.RouteConstraints); if (other.AttributeRouteModel != null) { AttributeRouteModel = new AttributeRouteModel(other.AttributeRouteModel); } }
public AttributeRouteModelValues(AttributeRouteModel inner) { if (inner != null) { Template = inner.Template; Order = inner.Order; Name = inner.Name; IsAbsoluteTemplate = inner.IsAbsoluteTemplate; } }
public AttributeRouteModel(AttributeRouteModel other) { if (other == null) { throw new ArgumentNullException(nameof(other)); } Attribute = other.Attribute; Name = other.Name; Order = other.Order; Template = other.Template; }
public void Combine_RightOverridesReflectedAttributeRouteModel( AttributeRouteModel left, AttributeRouteModel right) { // Arrange var expectedTemplate = AttributeRouteModel.CombineTemplates(null, right.Template); // Act var combined = AttributeRouteModel.CombineAttributeRouteModel(left, right); // Assert Assert.NotNull(combined); Assert.Equal(expectedTemplate, combined.Template); Assert.Equal(combined.Order, right.Order); }
public void ReplaceTokens_ValidValues(string template, object values, string expected) { // Arrange var valuesDictionary = values as IDictionary <string, object>; if (valuesDictionary == null) { valuesDictionary = new RouteValueDictionary(values); } // Act var result = AttributeRouteModel.ReplaceTokens(template, valuesDictionary); // Assert Assert.Equal(expected, result); }
/// <summary> /// Called to apply the convention to the <see cref="ApplicationModel"/>. /// </summary> /// <param name="application"> The<see cref="ApplicationModel"/>.</param> public virtual void Apply(ApplicationModel application) { var centralPrefix = new AttributeRouteModel(new RouteAttribute(template)); foreach (var controller in application.Controllers) { if (controller.AttributeRoutes.Count > 0) { for (var i = 0; i < controller.AttributeRoutes.Count; ++i) { controller.AttributeRoutes[i] = AttributeRouteModel.CombineAttributeRouteModel( centralPrefix, controller.AttributeRoutes[i]); } } else { controller.AttributeRoutes.Add(centralPrefix); } } }
public void CopyConstructor_CopiesAllProperties() { // Arrange var route = new AttributeRouteModel(new HttpGetAttribute("/api/Products")); route.Name = "products"; route.Order = 5; // Act var route2 = new AttributeRouteModel(route); // Assert foreach (var property in typeof(AttributeRouteModel).GetProperties()) { var value1 = property.GetValue(route); var value2 = property.GetValue(route2); if (typeof(IEnumerable <object>).IsAssignableFrom(property.PropertyType)) { Assert.Equal <object>((IEnumerable <object>)value1, (IEnumerable <object>)value2); // Ensure non-default value Assert.NotEmpty((IEnumerable <object>)value1); } else if (property.PropertyType.IsValueType || Nullable.GetUnderlyingType(property.PropertyType) != null) { Assert.Equal(value1, value2); // Ensure non-default value Assert.NotEqual(value1, Activator.CreateInstance(property.PropertyType)); } else { Assert.Same(value1, value2); // Ensure non-default value Assert.NotNull(value1); } } }
public void CopyConstructor_CopiesAllProperties() { // Arrange var route = new AttributeRouteModel(new HttpGetAttribute("/api/Products")); route.Name = "products"; route.Order = 5; // Act var route2 = new AttributeRouteModel(route); // Assert foreach (var property in typeof(AttributeRouteModel).GetProperties()) { var value1 = property.GetValue(route); var value2 = property.GetValue(route2); if (typeof(IEnumerable<object>).IsAssignableFrom(property.PropertyType)) { Assert.Equal<object>((IEnumerable<object>)value1, (IEnumerable<object>)value2); // Ensure non-default value Assert.NotEmpty((IEnumerable<object>)value1); } else if (property.PropertyType.IsValueType || Nullable.GetUnderlyingType(property.PropertyType) != null) { Assert.Equal(value1, value2); // Ensure non-default value Assert.NotEqual(value1, Activator.CreateInstance(property.PropertyType)); } else { Assert.Same(value1, value2); // Ensure non-default value Assert.NotNull(value1); } } }
public void ReplaceTokens_InvalidFormat(string template, object values, string reason) { // Arrange var valuesDictionary = values as IDictionary <string, object>; if (valuesDictionary == null) { valuesDictionary = new RouteValueDictionary(values); } var expected = string.Format( "The route template '{0}' has invalid syntax. {1}", template, reason); // Act var ex = Assert.Throws <InvalidOperationException>( () => { AttributeRouteModel.ReplaceTokens(template, valuesDictionary); }); // Assert Assert.Equal(expected, ex.Message); }
public void Apply(ApplicationModel application) { foreach (var controller in application.Controllers) { if (controller.AttributeRoutes.Count == 0) { //Create new attribute Route for the controller var attributeRouteModel = new AttributeRouteModel(); //Replace the . in the namespace with a / to create the attribute route //Ex: MySite.Admin namespace will correspond to MySite/Admin attribute route //Then attach [controller], [action] and optional {id?} token. //[Controller] and [action] is replaced with the controller and action //name to generate the final template var template = controller.ControllerType.Namespace.Replace('.', '/') + "/[controller]/[action]/{id?}"; attributeRouteModel.Template = template; controller.AttributeRoutes.Add(attributeRouteModel); } } //You can continue to put attribute route templates for the controller actions depending on the way you want them to behave }
public void ReplaceTokens_UnknownValue() { // Arrange var template = "[area]/[controller]/[action2]"; var values = new RouteValueDictionary() { { "area", "Help" }, { "controller", "Admin" }, { "action", "SeeUsers" }, }; var expected = "While processing template '[area]/[controller]/[action2]', " + "a replacement value for the token 'action2' could not be found. " + "Available tokens: 'action, area, controller'."; // Act var ex = Assert.Throws <InvalidOperationException>( () => { AttributeRouteModel.ReplaceTokens(template, values); }); // Assert Assert.Equal(expected, ex.Message); }
public ActionModel([NotNull] ActionModel other) { ActionMethod = other.ActionMethod; ActionName = other.ActionName; // Not making a deep copy of the controller, this action still belongs to the same controller. Controller = other.Controller; // These are just metadata, safe to create new collections ActionConstraints = new List <IActionConstraintMetadata>(other.ActionConstraints); Attributes = new List <object>(other.Attributes); Filters = new List <IFilter>(other.Filters); HttpMethods = new List <string>(other.HttpMethods); // Make a deep copy of other 'model' types. ApiExplorer = new ApiExplorerModel(other.ApiExplorer); Parameters = new List <ParameterModel>(other.Parameters.Select(p => new ParameterModel(p))); RouteConstraints = new List <IRouteConstraintProvider>(other.RouteConstraints); if (other.AttributeRouteModel != null) { AttributeRouteModel = new AttributeRouteModel(other.AttributeRouteModel); } }
private static string ChooseName( AttributeRouteModel left, AttributeRouteModel right) { if (right.Name == null && string.IsNullOrEmpty(right.Template)) { return left.Name; } else { return right.Name; } }
public void Combine_Orders( AttributeRouteModel left, AttributeRouteModel right, int? expected) { // Arrange & Act var combined = AttributeRouteModel.CombineAttributeRouteModel(left, right); // Assert Assert.NotNull(combined); Assert.Equal(expected, combined.Order); }
public void Combine_ValidReflectedAttributeRouteModels( AttributeRouteModel left, AttributeRouteModel right, AttributeRouteModel expectedResult) { // Arrange & Act var combined = AttributeRouteModel.CombineAttributeRouteModel(left, right); // Assert Assert.NotNull(combined); Assert.Equal(expectedResult.Template, combined.Template); }
public void Combine_NullOrNullTemplateReflectedAttributeRouteModels( AttributeRouteModel left, AttributeRouteModel right) { // Arrange & Act var combined = AttributeRouteModel.CombineAttributeRouteModel(left, right); // Assert Assert.Null(combined); }
public void Combine_Names( AttributeRouteModel left, AttributeRouteModel right, string expectedName) { // Arrange & Act var combined = AttributeRouteModel.CombineAttributeRouteModel(left, right); // Assert Assert.NotNull(combined); Assert.Equal(expectedName, combined.Name); }