How to call a PartialView from outside the controller in ASP.NET 5 -


i want create common message box method can called throughout asp.net 5 application

the code should this:

public class webhelper {     public static iactionresult messagebox(string message)     {         return partialview("~/views/shared/_messagebox.cshtml", message);     } } 

and in controller call:

return webhelper.messagebox("hallo world"); 

instead of doing of :

 return partialview("~/views/shared/_messagebox.cshtml", "hallo world"); 

however partialview bound controller seems cannot use outside controller.

i tried use answers 1 answers 2 solutions asp.net 4 don't work in asp.net 5 e.g.

 public string renderpartialviewtostring(string viewname, object model)     {         this.viewdata.model = model;         try         {             using (stringwriter sw = new stringwriter())             {                 viewengineresult viewresult = viewengines.engines.findpartialview(this.controllercontext, viewname);                 viewcontext viewcontext = new viewcontext(this.controllercontext, viewresult.view, this.viewdata, this.tempdata, sw);                 viewresult.view.render(viewcontext, sw);                  return sw.getstringbuilder().tostring();             }         }         catch (system.exception ex)         {             return ex.tostring();         }     } 

as can't find viewengines.engine in asp.net 5 namespaces.

question: simplest way solve problem in `asp.net 5? providing method , calling method code

here's helper class renders view: (as can see, viewengines.engine part couldn't find anymore injected irazorviewengine)

using system; using system.io; using microsoft.aspnet.http.internal; using microsoft.aspnet.mvc; using microsoft.aspnet.mvc.abstractions; using microsoft.aspnet.mvc.modelbinding; using microsoft.aspnet.mvc.razor; using microsoft.aspnet.mvc.rendering; using microsoft.aspnet.mvc.viewfeatures; using microsoft.aspnet.routing;  namespace webapp.demo.helpers {     public class razorviewtostringrenderer     {         private irazorviewengine _viewengine;         private itempdataprovider _tempdataprovider;         private iserviceprovider _serviceprovider;          public razorviewtostringrenderer(             irazorviewengine viewengine,             itempdataprovider tempdataprovider,             iserviceprovider serviceprovider)         {             _viewengine = viewengine;             _tempdataprovider = tempdataprovider;             _serviceprovider = serviceprovider;         }          public string renderviewtostring<tmodel>(string name, tmodel model)         {             var actioncontext = getactioncontext();              var viewengineresult = _viewengine.findview(actioncontext, name);              if (!viewengineresult.success)             {                 throw new invalidoperationexception(string.format("couldn't find view '{0}'", name));             }              var httpcontextaccessor = new httpcontextaccessor();              httpcontextaccessor.httpcontext = actioncontext.httpcontext;              var view = viewengineresult.view;              using (var output = new stringwriter())             {                 var viewcontext = new viewcontext(                     actioncontext,                     view,                     new viewdatadictionary<tmodel>(                         metadataprovider: new emptymodelmetadataprovider(),                         modelstate: new modelstatedictionary())                     {                         model = model                     },                     new tempdatadictionary(                         httpcontextaccessor,                         _tempdataprovider),                     output,                     new htmlhelperoptions());                  view.renderasync(viewcontext).getawaiter().getresult();                  return output.tostring();             }         }          private actioncontext getactioncontext()         {             var httpcontext = new defaulthttpcontext();             httpcontext.requestservices = _serviceprovider;             return new actioncontext(httpcontext, new routedata(), new actiondescriptor());         }     } } 

in startup.cs:

public void configureservices(iservicecollection services) {        // add mvc services services container.        services.addmvc();        // add helper rendering class singleton        services.addsingleton<razorviewtostringrenderer>(); } 

get injected helper class in controller example:

public homecontroller(razorviewtostringrenderer renderer) {        string message = "hallo world";         var partialview = renderer.renderviewtostring("~/views/shared/_messagebox.cshtml", message); } 

code here, few tweaks work old packages "microsoft.aspnet.mvc": "6.0.0-rc1-final": https://github.com/aspnet/entropy/tree/dev/samples/mvc.renderviewtostring

if you're using new "microsoft.aspnetcore.mvc": "1.0.0-*", code there


Comments