c# - IRazorViewEngine outside mvc -


i trying implement html template rendering using razor rendering engine. have windows service application partially uses mvc 6 , have found successful implementation works web part of application.

public viewrenderservice(irazorviewengine razorviewengine,     itempdataprovider tempdataprovider,     iserviceprovider serviceprovider) {     _razorviewengine = razorviewengine;     _tempdataprovider = tempdataprovider;     _serviceprovider = serviceprovider; }  public async task<string> rendertostringasync(string viewname, object model) {     var httpcontext = new defaulthttpcontext { requestservices = _serviceprovider };     var actioncontext = new actioncontext(httpcontext, new routedata(), new actiondescriptor());      using (var sw = new stringwriter())     {             var viewresult = _razorviewengine.findview(actioncontext, viewname, false);          if (viewresult.view == null)         {             throw new argumentnullexception($"{viewname} not match available view");         }          var viewdictionary = new viewdatadictionary(new emptymodelmetadataprovider(), new modelstatedictionary())         {             model = model         };          var viewcontext = new viewcontext(             actioncontext,             viewresult.view,             viewdictionary,             new tempdatadictionary(actioncontext.httpcontext, _tempdataprovider),             sw,             new htmlhelperoptions()         );          await viewresult.view.renderasync(viewcontext);         return sw.tostring();     } } 

my application not use web functionality if understanding correctly able use if web functionality disabled still need have webhostbuilder configured specify services di etc?

am correct assumption or there approach use?


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -