Hi, I want to render viewcomponent as string in the controller itself. Not in the cshtml.
Here is the code I'm using now but it gets me a null reference exception in the the InvokeAsyncCore inside DefaultViewComponentHelper which implement IViewComponentHelper :
public sealed class ViewComponentRender : IViewComponentRender { private readonly IViewComponentHelper _viewComponentHelper; public ViewComponentRender(IViewComponentHelper viewComponentHelper) { _viewComponentHelper = viewComponentHelper; } #region Implementation of IViewComponentRender public async Task<string> RenderToStringAsync(Type componentType, object arguments) {
//Exception in the line below : IHtmlContent htmlContent = await _viewComponentHelper.InvokeAsync(componentType, arguments); using (StringWriter stringWriter = new StringWriter()) { htmlContent.WriteTo(stringWriter, HtmlEncoder.Default); return stringWriter.ToString(); } } public async Task<string> RenderToStringAsync(Type componentType) {
//Exception in the line below : IHtmlContent htmlContent = await _viewComponentHelper.InvokeAsync(componentType); using (StringWriter stringWriter = new StringWriter()) { htmlContent.WriteTo(stringWriter, HtmlEncoder.Default); return stringWriter.ToString(); } } #endregion }