I want to create a common message box method that can be called throughout my ASP.NET 5
application
The code should be something like this:
public class WebHelper
{
public static IActionResult MessageBox(string message)
{
return PartialView("~/Views/Shared/_MessageBox.cshtml", message);
}
}
And then in my controller call:
return WebHelper.MessageBox("Hallo world");
instead of what I am currently doing of :
return PartialView("~/Views/Shared/_MessageBox.cshtml", "Hallo world");
However PartialView is bound to the controller so it seems I cannot use it outside the controller.
I tried to use Answers 1 Answers 2 which are solutions for ASP.NET 4
but they 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 I can't find the ViewEngines.Engine
in the ASP.NET 5
namespaces.
Question: What is the simplest way to solve my problem in `ASP.NET 5? Providing the method and calling method code