コード例 #1
0
        /// <summary>
        /// Combines two <see cref="ReflectedAttributeRouteModel"/> instances and returns
        /// a new <see cref="ReflectedAttributeRouteModel"/> instance with the result.
        /// </summary>
        /// <param name="left">The left <see cref="ReflectedAttributeRouteModel"/>.</param>
        /// <param name="right">The right <see cref="ReflectedAttributeRouteModel"/>.</param>
        /// <returns>A new instance of <see cref="ReflectedAttributeRouteModel"/> that represents the
        /// combination of the two <see cref="ReflectedAttributeRouteModel"/> instances or <c>null</c> if both
        /// parameters are <c>null</c>.</returns>
        public static ReflectedAttributeRouteModel CombineReflectedAttributeRouteModel(
            ReflectedAttributeRouteModel left,
            ReflectedAttributeRouteModel 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 ReflectedAttributeRouteModel()
            {
                Template = combinedTemplate,
                Order = right.Order ?? left.Order,
                Name = ChooseName(left, right),
            });
        }
コード例 #2
0
        public void Combine_InvalidTemplates(string left, string right, string expected)
        {
            // Arrange & Act
            var combined = ReflectedAttributeRouteModel.CombineTemplates(left, right);

            // Assert
            Assert.Equal(expected, combined);
        }
コード例 #3
0
        public void Combine_NullOrNullTemplateReflectedAttributeRouteModels(
            ReflectedAttributeRouteModel left,
            ReflectedAttributeRouteModel right)
        {
            // Arrange & Act
            var combined = ReflectedAttributeRouteModel.CombineReflectedAttributeRouteModel(left, right);

            // Assert
            Assert.Null(combined);
        }
コード例 #4
0
        public void Combine_Names(
            ReflectedAttributeRouteModel left,
            ReflectedAttributeRouteModel right,
            string expectedName)
        {
            // Arrange & Act
            var combined = ReflectedAttributeRouteModel.CombineReflectedAttributeRouteModel(left, right);

            // Assert
            Assert.NotNull(combined);
            Assert.Equal(expectedName, combined.Name);
        }
コード例 #5
0
        public void Combine_ValidReflectedAttributeRouteModels(
            ReflectedAttributeRouteModel left,
            ReflectedAttributeRouteModel right,
            ReflectedAttributeRouteModel expectedResult)
        {
            // Arrange & Act
            var combined = ReflectedAttributeRouteModel.CombineReflectedAttributeRouteModel(left, right);

            // Assert
            Assert.NotNull(combined);
            Assert.Equal(expectedResult.Template, combined.Template);
        }
コード例 #6
0
        public void Combine_Orders(
            ReflectedAttributeRouteModel left,
            ReflectedAttributeRouteModel right,
            int?expected)
        {
            // Arrange & Act
            var combined = ReflectedAttributeRouteModel.CombineReflectedAttributeRouteModel(left, right);

            // Assert
            Assert.NotNull(combined);
            Assert.Equal(expected, combined.Order);
        }
コード例 #7
0
 private static string ChooseName(
     ReflectedAttributeRouteModel left,
     ReflectedAttributeRouteModel right)
 {
     if (right.Name == null && string.IsNullOrEmpty(right.Template))
     {
         return(left.Name);
     }
     else
     {
         return(right.Name);
     }
 }
コード例 #8
0
        public void Combine_RightOverridesReflectedAttributeRouteModel(
            ReflectedAttributeRouteModel left,
            ReflectedAttributeRouteModel right)
        {
            // Arrange
            var expectedTemplate = ReflectedAttributeRouteModel.CombineTemplates(null, right.Template);

            // Act
            var combined = ReflectedAttributeRouteModel.CombineReflectedAttributeRouteModel(left, right);

            // Assert
            Assert.NotNull(combined);
            Assert.Equal(expectedTemplate, combined.Template);
            Assert.Equal(combined.Order, right.Order);
        }
コード例 #9
0
        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 = ReflectedAttributeRouteModel.ReplaceTokens(template, valuesDictionary);

            // Assert
            Assert.Equal(expected, result);
        }
コード例 #10
0
ファイル: ReflectedActionModel.cs プロジェクト: erarijit/Mvc
        public ReflectedActionModel([NotNull] MethodInfo actionMethod)
        {
            ActionMethod = actionMethod;

            // CoreCLR returns IEnumerable<Attribute> from GetCustomAttributes - the OfType<object>
            // is needed to so that the result of ToList() is List<object>
            Attributes = actionMethod.GetCustomAttributes(inherit: true).OfType <object>().ToList();

            Filters = Attributes
                      .OfType <IFilter>()
                      .ToList();

            var routeTemplateAttribute = Attributes.OfType <IRouteTemplateProvider>().FirstOrDefault();

            if (routeTemplateAttribute != null)
            {
                AttributeRouteModel = new ReflectedAttributeRouteModel(routeTemplateAttribute);
            }

            HttpMethods = new List <string>();
            Parameters  = new List <ReflectedParameterModel>();
        }
コード例 #11
0
        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>(
                () => { ReflectedAttributeRouteModel.ReplaceTokens(template, valuesDictionary); });

            // Assert
            Assert.Equal(expected, ex.Message);
        }
コード例 #12
0
        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: 'area, controller, action'.";

            // Act
            var ex = Assert.Throws <InvalidOperationException>(
                () => { ReflectedAttributeRouteModel.ReplaceTokens(template, values); });

            // Assert
            Assert.Equal(expected, ex.Message);
        }