Пример #1
0
        private async Task ExecutePageAsync()
        {
            var modelType    = _actionDescriptor.ModelType?.AsType();
            var viewDataType = typeof(ViewDataDictionary <>).MakeGenericType(modelType ?? _actionDescriptor.PageType.AsType());

            var viewData = (ViewDataDictionary)Activator.CreateInstance(viewDataType, _metadataProvider, _actionContext.ModelState);

            var tempData = _tempDataFactory.GetTempData(_actionContext.HttpContext);

            var pageContext = new PageContext(_actionContext, viewData, tempData, _viewOptions.HtmlHelperOptions)
            {
                ActionDescriptor       = _actionDescriptor,
                ValueProviderFactories = _valueProviderFactories,
            };

            var page = (Page)_factory.CreatePage(pageContext);

            pageContext.Page = page;

            object model = null;

            if (modelType == null)
            {
                model = page;
            }
            else
            {
                model = _modelFactory.CreateModel(pageContext);
            }

            if (model != null)
            {
                viewData.Model = model;
            }

            var           tempDataPropertyTracker = _tempDataPropertyProvider.LoadAndTrackChanges(page, page.TempData);
            IActionResult result = null;

            var handler = _selector.Select(pageContext);

            if (handler != null)
            {
                var executor = ExecutorFactory.Create(handler.Method);
                result = await executor(page, model);
            }

            if (result == null)
            {
                result = new PageViewResult(page);
            }

            await result.ExecuteResultAsync(pageContext);

            tempDataPropertyTracker.SaveChanges();
        }
Пример #2
0
        private async Task ExecutePageAsync()
        {
            Type viewDataType     = null;
            var  viewDataProperty = _actionDescriptor.PageType.GetDeclaredProperty("ViewData");

            if (viewDataProperty != null)
            {
                var viewDataPropertyType = viewDataProperty.PropertyType.GetTypeInfo();
                if (viewDataPropertyType.IsGenericType && viewDataPropertyType.GetGenericTypeDefinition() == typeof(ViewDataDictionary <>))
                {
                    viewDataType = viewDataPropertyType.GetGenericArguments()[0];
                }
            }

            ViewDataDictionary viewData;

            if (viewDataType == null)
            {
                viewData = new ViewDataDictionary <object>(_metadataProvider, _actionContext.ModelState);
            }
            else
            {
                viewData = (ViewDataDictionary)Activator.CreateInstance(typeof(ViewDataDictionary <>).MakeGenericType(viewDataType), _metadataProvider, _actionContext.ModelState);
            }

            var tempData = _tempDataFactory.GetTempData(_actionContext.HttpContext);

            var pageContext = new PageContext(_actionContext, viewData, tempData, _viewOptions.HtmlHelperOptions)
            {
                ActionDescriptor       = _actionDescriptor,
                ValueProviderFactories = _valueProviderFactories,
            };

            var page = (Page)_factory.CreatePage(pageContext);

            var           tempDataPropertyTracker = _tempDataPropertyProvider.LoadAndTrackChanges(page, page.TempData);
            IActionResult result = null;

            var handler = _selector.Select(pageContext);

            if (handler != null)
            {
                var executor = ExecutorFactory.Create(handler.Method);
                result = await executor(page);
            }

            if (result == null)
            {
                result = new PageViewResult(page);
            }

            await result.ExecuteResultAsync(pageContext);

            tempDataPropertyTracker.SaveChanges();
        }