Exemple #1
0
        async Task <IHtmlContent> InvokeViewComponentAsync(string viewName, object arguments)
        {
            if (!(_viewComponentHelper is DefaultViewComponentHelper helper))
            {
                throw new ArgumentNullException(
                          $"{_viewComponentHelper.GetType()} cannot be converted to DefaultViewComponentHelper");
            }

            // Contextualize view component
            helper.Contextualize(ViewContext);

            // Log the invocation, we can't use try / catch around our view component helper :(
            if (_logger.IsEnabled(LogLevel.Information))
            {
                _logger.LogInformation($"Attempting to invoke view component \"{viewName}\".");
            }

            try
            {
                return(await _viewComponentHelper.InvokeAsync(viewName, arguments));
            }
            catch (Exception e)
            {
                if (_logger.IsEnabled(LogLevel.Error))
                {
                    _logger.LogError(e,
                                     $"An exception occurred whilst invoking the view component with name \"{viewName}\". {e.Message}");
                }
                throw;
            }
        }
Exemple #2
0
        public async Task <IHtmlContent> InvokeAsync(Abstractions.IView view)
        {
            // We always need a view name to invoke
            if (string.IsNullOrEmpty(view.ViewName))
            {
                throw new ArgumentNullException(nameof(view.ViewName));
            }

            if (!(_viewComponentHelper is DefaultViewComponentHelper helper))
            {
                throw new ArgumentNullException(
                          $"{_viewComponentHelper.GetType()} cannot be converted to DefaultViewComponentHelper");
            }

            // Contextualize view component
            helper.Contextualize(ViewContext);

            // Log the invocation
            if (_logger.IsEnabled(LogLevel.Information))
            {
                _logger.LogInformation($"Attempting to invoke view component \"{view.ViewName}\".");
            }

            try
            {
                return(await _viewComponentHelper.InvokeAsync(view.ViewName, view.Model));
            }
            catch (Exception e)
            {
                if (_logger.IsEnabled(LogLevel.Error))
                {
                    _logger.LogError(e,
                                     $"An exception occurred whilst invoking the view component with name \"{view.ViewName}\". {e.Message}");
                }
                throw;
            }
        }