Beispiel #1
0
            /// <summary>
            /// Called asynchronously before the action, after model binding is complete.
            /// </summary>
            /// <param name="context">A context for action filters</param>
            /// <returns>A task that represents the asynchronous operation</returns>
            private async Task PublishModelReceivedEventAsync(ActionExecutingContext context)
            {
                if (context == null)
                {
                    throw new ArgumentNullException(nameof(context));
                }

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

                if (IgnoreFilter(context))
                {
                    return;
                }

                //model received event
                foreach (var model in context.ActionArguments.Values.OfType <BaseNopModel>())
                {
                    //we publish the ModelReceived event for all models as the BaseNopModel,
                    //so you need to implement IConsumer<ModelReceived<BaseNopModel>> interface to handle this event
                    await _eventPublisher.ModelReceivedAsync(model, context.ModelState);
                }
            }
            /// <summary>
            /// Called asynchronously before the action, after model binding is complete.
            /// </summary>
            /// <param name="context">A context for action filters</param>
            /// <returns>A task that on completion indicates the necessary filter actions have been executed</returns>
            private async Task PublishModelReceivedEventAsync(ActionExecutingContext context)
            {
                if (context == null)
                {
                    throw new ArgumentNullException(nameof(context));
                }

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

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

                //check whether this filter has been overridden for the Action
                var 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;
                }

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