コード例 #1
0
            /// <summary>
            /// Called after the action executes, before the action result
            /// </summary>
            /// <param name="context">A context for action filters</param>
            public void OnActionExecuted(ActionExecutedContext context)
            {
                if (context == null)
                {
                    throw new ArgumentNullException(nameof(context));
                }

                //check whether this filter has been overridden for the Action
                PublishModelEventsAttribute actionFilter = context.ActionDescriptor.FilterDescriptors
                                                           .Where(filterDescriptor => filterDescriptor.Scope == FilterScope.Action)
                                                           .Select(filterDescriptor => filterDescriptor.Filter).OfType <PublishModelEventsAttribute>().FirstOrDefault();

                //whether to ignore this filter
                if (actionFilter?.IgnoreFilter ?? _ignoreFilter)
                {
                    return;
                }

                if (context.HttpContext.Request == null)
                {
                    return;
                }

                //model prepared event
                if (context.Controller is Microsoft.AspNetCore.Mvc.Controller controller)
                {
                    if (controller.ViewData.Model is BaseAgsModel model)
                    {
                        //we publish the ModelPrepared event for all models as the BaseAgsModel,
                        //so you need to implement IConsumer<ModelPrepared<BaseAgsModel>> interface to handle this event
                        _eventPublisher.ModelPrepared(model);
                    }

                    if (controller.ViewData.Model is IEnumerable <BaseAgsModel> modelCollection)
                    {
                        //we publish the ModelPrepared event for collection as the IEnumerable<BaseAgsModel>,
                        //so you need to implement IConsumer<ModelPrepared<IEnumerable<BaseAgsModel>>> interface to handle this event
                        _eventPublisher.ModelPrepared(modelCollection);
                    }
                }
            }
コード例 #2
0
            /// <summary>
            /// Called before the action executes, after model binding is complete
            /// </summary>
            /// <param name="context">A context for action filters</param>
            public void OnActionExecuting(ActionExecutingContext context)
            {
                if (context == null)
                {
                    throw new ArgumentNullException(nameof(context));
                }

                //check whether this filter has been overridden for the Action
                PublishModelEventsAttribute actionFilter = context.ActionDescriptor.FilterDescriptors
                                                           .Where(filterDescriptor => filterDescriptor.Scope == FilterScope.Action)
                                                           .Select(filterDescriptor => filterDescriptor.Filter).OfType <PublishModelEventsAttribute>().FirstOrDefault();

                //whether to ignore this filter
                if (actionFilter?.IgnoreFilter ?? _ignoreFilter)
                {
                    return;
                }

                if (context.HttpContext.Request == null)
                {
                    return;
                }

                //only in POST requests
                if (!context.HttpContext.Request.Method.Equals(WebRequestMethods.Http.Post, StringComparison.InvariantCultureIgnoreCase))
                {
                    return;
                }

                //model received event
                foreach (BaseAgsModel model in context.ActionArguments.Values.OfType <BaseAgsModel>())
                {
                    //we publish the ModelReceived event for all models as the BaseAgsModel,
                    //so you need to implement IConsumer<ModelReceived<BaseAgsModel>> interface to handle this event
                    _eventPublisher.ModelReceived(model, context.ModelState);
                }
            }