2

I have ASP.NET 4.6 Web-Forms and ASP.NET Core websites. And there is a class Library project which is shared amongst them. My purpose is to include the Master Page related stuff into the View Component that resides into the Class Library.

I have created the master page View Component in the Class Library, and successfully able to invoke it into the shared _Layout view of ASP.NET Core website. Below is the blog which I followed for it.

Reusing External View Components

My next task is to somehow get the flatten HTML of the master page View Component from the Class Library into the ASP.NET 4.6 web forms. I read couple of stack overflow questions, and came across the following one, which shows how to render the Razor View to string in ASP.NET Core.

Render Razor View to string in ASP.NET Core

Here is the code snippet from the answer, and I can confirm that, it works fine, provided I put this code in the Controller class of the ASP.NET Core.

        public string RenderPartialViewToString(string viewName, object model)
        {
            if (string.IsNullOrEmpty(viewName))
                viewName = ActionContext.ActionDescriptor.Name;

            ViewData.Model = model;

            using (StringWriter sw = new StringWriter())
            {
                var engine = Resolver.GetService(typeof(ICompositeViewEngine)) as ICompositeViewEngine;
                ViewEngineResult viewResult = engine.FindPartialView(ActionContext, viewName);

                ViewContext viewContext = new ViewContext(ActionContext, viewResult.View, 
                ViewData, TempData, sw, new HtmlHelperOptions());

                var t = viewResult.View.RenderAsync(viewContext);
                t.Wait();

                return sw.GetStringBuilder().ToString();
            }
        }

My plan is to move this code into the Class Library project. But, if you see the dependencies, it needs Resolver (IServiceProvider) which is the public property available in the base class (Microsoft.AspNet.Mvc.Controller).

My question is how can I inject the Resolver or the Razor View Engine from ASP.NET Core into the Class Library project. So that, when ASP.NET 4.6 Web-Forms requests the flatten HTML for the View Component, the Class Library serves it hassle free.

Community
  • 1
  • 1
Mukesh Bhojwani
  • 2,014
  • 4
  • 20
  • 35
  • dependency injection works the same in a class library as anywhere else, you just take a constructor dependency in your class library class for what you need, and make sure you are also injecting your class wherever you need it. as long as all the dependencies are registered and you use dependency injection all the way down, ie use DI for everything and don't new up your objects, it should work fine – Joe Audette Jun 01 '16 at 10:54
  • fyi I have [a class here](https://github.com/joeaudette/cloudscribe/blob/master/src/cloudscribe.Web.Common/Razor/ViewRenderer.cs) that can render razor views as a string, and it uses constructor dependencies to get what it needs from DI. I use it in this other project so that I can [generate html email using razor](https://github.com/joeaudette/cloudscribe.SimpleContent/blob/master/src/cloudscribe.SimpleContent.Web/Services/ProjectEmailService.cs) – Joe Audette Jun 01 '16 at 18:29
  • Hi Joe, thanks for your inputs. Does your ViewRenderer class resides in Class Library? if yes, how do you request it's instance, such that all the necessary dependencies are fulfilled in it? I doubt, when I have the class library as compiled DLL into my ASP.NET 4.6 Web-Forms, the context would be filled through contextAccessor. – Mukesh Bhojwani Jun 02 '16 at 04:35
  • I don't know about integration with ASP.NET WebForms. with ASP.NET Core, my controller receives an emailsender class from DI by its constructor, EmailSender receives the ViewRenderer via its constructor, ViewRenderer receives what it needs for its constructor. It is dependency injection all the way for everything. just have to register all the needed dependencies in startup. not sure if you can do that in webforms somehow – Joe Audette Jun 02 '16 at 09:51

0 Answers0