I am migrating a webform (not a WebForm technology, a stripped down Web Application into a webform functionality) from ASP.NET MVC to ASP.NET Core MVC. My current biggest problem is a static class that we had in the previous version of the webform. This static class uses packages that were available in .NET but not in .NET Core.
I understand that for some of the methods in this static class, I have to use dependency injection to resolve the package problems. However, it is not possible to pass a parameter to a static class making this an "antipattern" for .NET Core.
My Utils.cs static class has only two methods, RenderPartialToString
and SendEmail
. SendEmail
is very simple and has no problems with the current .NET Core packages. However, I have the following code in my static class that does not work with current version.
public static class Utils
{
public static readonly string ApiUrl = ConfigurationManager.AppSettings["ApiUrl"];
public static readonly string ApiKey = ConfigurationManager.AppSettings["ApiKey"];
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() + "');";
}
}
...
}
ViewEngine and ConfigurationManager are not available in .NET Core making this static class very difficult to migrate. I believe, I could implement both of these features with dependency injection. However, I do not know how to change this static class so that I can use dependency injection and be able to use these methods in my Controllers.
How can I simply migrate this static class into .NET Core for some dependency injection implementation? Do I need to change all the instances of the Utils class and make it not static?