예제 #1
0
        public void CreateViewDataDictionary_UsesDeclaredTypeOverModelType_WhenCreatingTheViewDataDictionary()
        {
            // Arrange
            var modelMetadataProvider = new EmptyModelMetadataProvider();
            var activator             = new RazorPagePropertyActivator(
                typeof(TestPage),
                declaredModelType: typeof(TestModel),
                metadataProvider: modelMetadataProvider,
                propertyValueAccessors: null);
            var original = new ViewDataDictionary(modelMetadataProvider, new ModelStateDictionary())
            {
                { "test-key", "test-value" },
            };
            var viewContext = new ViewContext
            {
                ViewData = original,
            };

            // Act
            var viewDataDictionary = activator.CreateViewDataDictionary(viewContext);

            // Assert
            Assert.NotNull(viewDataDictionary);
            Assert.NotSame(original, viewDataDictionary);
            Assert.IsType <ViewDataDictionary <TestModel> >(viewDataDictionary);
            Assert.Equal("test-value", viewDataDictionary["test-key"]);
        }
예제 #2
0
        public void CreateViewDataDictionary_ReturnsInstanceOnContext_IfModelTypeMatches()
        {
            // Arrange
            var modelMetadataProvider = new EmptyModelMetadataProvider();
            var activator             = new RazorPagePropertyActivator(
                typeof(TestPage),
                declaredModelType: typeof(TestModel),
                metadataProvider: modelMetadataProvider,
                propertyValueAccessors: null);
            var original = new ViewDataDictionary <TestModel>(modelMetadataProvider, new ModelStateDictionary())
            {
                { "test-key", "test-value" },
            };
            var viewContext = new ViewContext
            {
                ViewData = original,
            };

            // Act
            var viewDataDictionary = activator.CreateViewDataDictionary(viewContext);

            // Assert
            Assert.NotNull(viewDataDictionary);
            Assert.Same(original, viewDataDictionary);
        }
예제 #3
0
        /// <inheritdoc />
        public void Activate(IRazorPage page, ViewContext context)
        {
            if (page == null)
            {
                throw new ArgumentNullException(nameof(page));
            }

            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var pageType = page.GetType();
            RazorPagePropertyActivator propertyActivator;

            if (!_activationInfo.TryGetValue(pageType, out propertyActivator))
            {
                // Look for a property named "Model". If it is non-null, we'll assume this is
                // the equivalent of TModel Model property on RazorPage<TModel>.
                //
                // Otherwise if we don't have a model property the activator will just skip setting
                // the view data.
                var modelType = pageType.GetRuntimeProperty(ModelPropertyName)?.PropertyType;
                propertyActivator = new RazorPagePropertyActivator(
                    pageType,
                    modelType,
                    _metadataProvider,
                    _propertyAccessors);

                propertyActivator = _activationInfo.GetOrAdd(pageType, propertyActivator);
            }

            propertyActivator.Activate(page, context);
        }
예제 #4
0
        public void CreateViewDataDictionary_MakesNewInstanceWithObjectModelType_WhenValueOnContextAndModelTypeAreNull()
        {
            // Arrange
            var activator = new RazorPagePropertyActivator(
                typeof(TestPage),
                declaredModelType: null,
                metadataProvider: new EmptyModelMetadataProvider(),
                propertyValueAccessors: null);
            var viewContext = new ViewContext();

            // Act
            var viewDataDictionary = activator.CreateViewDataDictionary(viewContext);

            // Assert
            Assert.NotNull(viewDataDictionary);
            Assert.IsType <ViewDataDictionary <object> >(viewDataDictionary);
        }
예제 #5
0
        public void CreateViewDataDictionary_MakesNewInstance_WhenValueOnContextIsNull()
        {
            // Arrange
            var activator = new RazorPagePropertyActivator(
                typeof(TestPage),
                typeof(TestModel),
                new EmptyModelMetadataProvider(),
                propertyValueAccessors: null);
            var viewContext = new ViewContext();

            // Act
            var viewDataDictionary = activator.CreateViewDataDictionary(viewContext);

            // Assert
            Assert.NotNull(viewDataDictionary);
            Assert.IsType <ViewDataDictionary <TestModel> >(viewDataDictionary);
        }
예제 #6
0
        internal RazorPagePropertyActivator GetOrAddCacheEntry(IRazorPage page)
        {
            var  pageType          = page.GetType();
            Type?providedModelType = null;

            if (page is IModelTypeProvider modelTypeProvider)
            {
                providedModelType = modelTypeProvider.GetModelType();
            }

            // We only need to vary by providedModelType since it varies at runtime. Defined model type
            // is synonymous with the pageType and consequently does not need to be accounted for in the cache key.
            var cacheKey = new CacheKey(pageType, providedModelType);

            if (!_activationInfo.TryGetValue(cacheKey, out var propertyActivator))
            {
                // Look for a property named "Model". If it is non-null, we'll assume this is
                // the equivalent of TModel Model property on RazorPage<TModel>.
                //
                // Otherwise if we don't have a model property the activator will just skip setting
                // the view data.
                var modelType = providedModelType;
                if (modelType == null)
                {
                    modelType = pageType.GetRuntimeProperty(ModelPropertyName)?.PropertyType;
                }

                propertyActivator = new RazorPagePropertyActivator(
                    pageType,
                    modelType,
                    _metadataProvider,
                    _propertyAccessors);

                propertyActivator = _activationInfo.GetOrAdd(cacheKey, propertyActivator);
            }

            return(propertyActivator);
        }