I am migrating a web form (not WebForm technology, a truncated web application into web form functionality) from ASP.NET MVC to ASP.NET Core MVC. My biggest problem is the static class that we had in the previous version of the web form. This static class uses packages that were available in .NET but not in .NET Core.
I understand that for some methods in this static class, I have to use dependency injection to solve package problems. However, it is not possible to pass a parameter to a static class, making it "antipattern" for .NET Core.
The static class My Utils.cs has only two methods: RenderPartialToStringand SendEmail. SendEmailvery simple and has no problems with current .NET Core packages. However, I have the following code in my static class that does not work with the 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, so this static class is very difficult to port. I believe that I could implement both of these functions using dependency injection. However, I don't 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 just port this static class to .NET Core for any dependency injection implementation? Do I need to change all instances of the Utils class and make it non-static?