I am trying to use ICompositeViewEngine in ASP.NET Core MVC to replace ViewEngine from System.Web.Mvc as it is no longer available in .NET Core. I usually try to migrate a web form from ASP.NET to ASP.NET Core in this project.
I found the following solution: Where are the ControllerContext and ViewEngines properties in MVC 6 Controller? , and I believe that this can solve my problem. I also found a similar engine creation with ServiceProvider in the github question: https://github.com/aspnet/Mvc/issues/3091
However, I'm not sure which dependencies or frameworks I can lose, as I am very new to .NET. I have the following namespaces:
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewEngines;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.Extensions.DependencyInjection;
I believe this may be due to my problem.
My source code:
public static string RenderPartialToString(Controller controller, string viewName, object model)
{
controller.ViewData.Model = model;
using (StringWriter sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
ViewContext viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
viewResult.View.Render(viewContext, sw);
return "document.write('" + sw.GetStringBuilder().Replace('\n', ' ').Replace('\r', ' ').Replace("'","\\'").ToString() + "');";
}
}
And now I'm trying to use one of the following:
var engine = Resolver.GetService(typeof(ICompositeViewEngine)) as ICompositeViewEngine;
var engine2 = IServiceProvider.GetService(typeof(ICompositeViewEngine)) as ICompositeViewEngine;
Am I on the right track to fix this? Is there an easier way to replace System.Web.Mvc ViewEngines in .NET Core? What do I need to fix " does not exist in the current context " for Resolver and / or ServiceProvider?
Thank. I hope I was able to follow the recommendations on the issues.
Edit: Please let me know if I should include anything else from my code for this question. I am currently reading about Injection Dependency to better understand the situation.