public virtual Func <PageContext, ViewContext, object> CreatePageFactory(CompiledPageActionDescriptor actionDescriptor)
        {
            if (!typeof(PageBase).GetTypeInfo().IsAssignableFrom(actionDescriptor.PageTypeInfo))
            {
                throw new InvalidOperationException(Resources.FormatActivatedInstance_MustBeAnInstanceOf(
                                                        _pageActivator.GetType().FullName,
                                                        typeof(PageBase).FullName));
            }

            var activatorFactory  = _pageActivator.CreateActivator(actionDescriptor);
            var modelType         = actionDescriptor.ModelTypeInfo?.AsType() ?? actionDescriptor.PageTypeInfo.AsType();
            var propertyActivator = new RazorPagePropertyActivator(
                actionDescriptor.PageTypeInfo.AsType(),
                modelType,
                _modelMetadataProvider,
                _propertyAccessors);

            return((pageContext, viewContext) =>
            {
                var page = (PageBase)activatorFactory(pageContext, viewContext);
                page.PageContext = pageContext;
                page.Path = pageContext.ActionDescriptor.RelativePath;
                page.ViewContext = viewContext;
                propertyActivator.Activate(page, viewContext);
                return page;
            });
        }
    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"]);
    }
    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);
    }
    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);
    }
    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);
    }
Exemple #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);
    }