示例#1
0
        public async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next)
        {
            if (context.HttpContext.Request.Method.Equals("POST") || context.HttpContext.Request.Method.Equals("PUT"))
            {
                if (!context.ModelState.IsValid)
                {
                    if (context.HttpContext.Request.IsAjaxRequest())
                    {
                        var errorModel = context.ModelState.Keys.Where(x => context.ModelState[x].Errors.Count > 0)
                                         .Select(x => new
                        {
                            key    = x,
                            errors = context.ModelState[x].Errors.Select(y => y.ErrorMessage).ToArray()
                        });

                        context.Result = new JsonResult(new AjaxResultHelper <IEnumerable <object> >
                        {
                            Response = errorModel,
                            Message  = "_InvalidData_"
                        });
                    }
                    else
                    {
                        var result = await next();

                        var provider = context.HttpContext.RequestServices.GetRequiredService <IModelMetadataProvider>();
                        var viewDataDictionaryModelType = context.ActionDescriptor.DeclaredModelTypeInfo ?? typeof(object);

                        //if (viewDataDictionaryModelType != null)
                        //{
                        //    Type _viewDataDictionaryType = typeof(ViewDataDictionary<>).MakeGenericType(viewDataDictionaryModelType);
                        //    _rootFactory = ViewDataDictionaryFactory.CreateFactory(viewDataDictionaryModelType.GetTypeInfo());
                        //    _nestedFactory = ViewDataDictionaryFactory.CreateNestedFactory(viewDataDictionaryModelType.GetTypeInfo());
                        //}

                        var factory        = ViewDataDictionaryFactory.CreateFactory(viewDataDictionaryModelType.GetTypeInfo());
                        var _nestedFactory = ViewDataDictionaryFactory.CreateNestedFactory(viewDataDictionaryModelType.GetTypeInfo());

                        var viewData    = factory(provider, context.ModelState);
                        var viewData2   = _nestedFactory(viewData);
                        var contentType = context.HttpContext.Request.ContentType;

                        context.Result = new PageResult
                        {
                            ViewData    = viewData,
                            ContentType = contentType,
                            StatusCode  = 400,
                        };
                    }
                }
            }
            else
            {
                await next.Invoke();
            }
        }
示例#2
0
        private PageActionInvokerCacheEntry CreateCacheEntry(
            ActionInvokerProviderContext context,
            FilterItem[] cachedFilters)
        {
            var compiledActionDescriptor = (CompiledPageActionDescriptor)context.ActionContext.ActionDescriptor;

            var viewDataFactory = ViewDataDictionaryFactory.CreateFactory(compiledActionDescriptor.DeclaredModelTypeInfo);

            var pageFactory    = _pageFactoryProvider.CreatePageFactory(compiledActionDescriptor);
            var pageDisposer   = _pageFactoryProvider.CreatePageDisposer(compiledActionDescriptor);
            var propertyBinder = PageBinderFactory.CreatePropertyBinder(
                _parameterBinder,
                _modelMetadataProvider,
                _modelBinderFactory,
                compiledActionDescriptor);

            Func <PageContext, object>   modelFactory  = null;
            Action <PageContext, object> modelReleaser = null;

            if (compiledActionDescriptor.ModelTypeInfo != compiledActionDescriptor.PageTypeInfo)
            {
                modelFactory  = _modelFactoryProvider.CreateModelFactory(compiledActionDescriptor);
                modelReleaser = _modelFactoryProvider.CreateModelDisposer(compiledActionDescriptor);
            }

            var viewStartFactories = GetViewStartFactories(compiledActionDescriptor);

            var handlerExecutors = GetHandlerExecutors(compiledActionDescriptor);
            var handlerBinders   = GetHandlerBinders(compiledActionDescriptor);

            return(new PageActionInvokerCacheEntry(
                       compiledActionDescriptor,
                       viewDataFactory,
                       pageFactory,
                       pageDisposer,
                       modelFactory,
                       modelReleaser,
                       propertyBinder,
                       handlerExecutors,
                       handlerBinders,
                       viewStartFactories,
                       cachedFilters));
        }
示例#3
0
    public RazorPagePropertyActivator(
        Type pageType,
        Type?declaredModelType,
        IModelMetadataProvider metadataProvider,
        PropertyValueAccessors propertyValueAccessors)
    {
        _metadataProvider = metadataProvider;

        // In the absence of a model on the current type, we'll attempt to use ViewDataDictionary<object> on the current type.
        var viewDataDictionaryModelType = declaredModelType ?? typeof(object);

        _viewDataDictionaryType = typeof(ViewDataDictionary <>).MakeGenericType(viewDataDictionaryModelType);
        _rootFactory            = ViewDataDictionaryFactory.CreateFactory(viewDataDictionaryModelType);
        _nestedFactory          = ViewDataDictionaryFactory.CreateNestedFactory(viewDataDictionaryModelType);

        _propertyActivators = PropertyActivator <ViewContext> .GetPropertiesToActivate(
            pageType,
            typeof(RazorInjectAttribute),
            propertyInfo => CreateActivateInfo(propertyInfo, propertyValueAccessors),
            includeNonPublic : true);
    }
示例#4
0
        public RazorPagePropertyActivator(
            Type pageType,
            Type modelType,
            IModelMetadataProvider metadataProvider,
            PropertyValueAccessors propertyValueAccessors)
        {
            _metadataProvider = metadataProvider;


            if (modelType != null)
            {
                _viewDataDictionaryType = typeof(ViewDataDictionary <>).MakeGenericType(modelType);
                _rootFactory            = ViewDataDictionaryFactory.CreateFactory(modelType.GetTypeInfo());
                _nestedFactory          = ViewDataDictionaryFactory.CreateNestedFactory(modelType.GetTypeInfo());
            }

            _propertyActivators = PropertyActivator <ViewContext> .GetPropertiesToActivate(
                pageType,
                typeof(RazorInjectAttribute),
                propertyInfo => CreateActivateInfo(propertyInfo, propertyValueAccessors),
                includeNonPublic : true);
        }