private IViewComponentHelper GetViewComponentHelper(MyViewComponentContext context, StringWriter sw)
        {
            var viewContext = new ViewContext(context.ActionContext, NullView.Instance, context.ViewData, context.TempData, sw, new HtmlHelperOptions());
            var helper      = context.HttpContext.RequestServices.GetRequiredService <IViewComponentHelper>();

            (helper as IViewContextAware)?.Contextualize(viewContext);
            return(helper);
        }
        private async Task <string> Render(MyViewComponentContext myViewComponentContext, string viewComponentName, object args)
        {
            using (var writer = new StringWriter())
            {
                var helper = this.GetViewComponentHelper(myViewComponentContext, writer);
                var result = await helper.InvokeAsync(viewComponentName, args);

                result.WriteTo(writer, HtmlEncoder.Default);
                await writer.FlushAsync();

                return(writer.ToString());
            }
        }
        public Task <string> RenderToStringAsync(string viewName, object model)
        {
            var httpContext   = this._httpContextAccessor.HttpContext;
            var actionContext = new ActionContext(httpContext, httpContext.GetRouteData(), _actionContext.ActionContext.ActionDescriptor);
            var context       = new MyViewComponentContext {
                HttpContext   = httpContext,
                ActionContext = actionContext,
                ViewData      = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary())
                {
                    Model = model
                },
                TempData = new TempDataDictionary(
                    httpContext,
                    _tempDataProvider
                    ),
            };

            return(this.Render(context, viewName, model));
        }