I can't find a way to build a ViewResult from a simple html string. I tried many solutions from simple to more elaborated, but it seems that I always lose the (master page / layout) in the process.
Here is a solution that works, obviously since I don't do anything special here :
public class MyActionResult : ViewResult
{
public MyActionResult(controller controller, object model)
{
this.ViewName = "MyView";
this.ViewData.Model = model;
}
public override void ExecuteResult(ControllerContext context)
{
base.ExecuteResult(context);
}
}
From this working result that is equal to just calling View("MyView", model) from the action, I went to :
public class MyActionResult : ViewResult
{
public MyActionResult(GenericController controller, object model)
{
this.View = new MyView(controller, model);
}
public override void ExecuteResult(ControllerContext context)
{
base.ExecuteResult(context);
}
}
public class MyView : IView
{
public MyView(GenericController controller, object model)
{
}
public void Render(ViewContext viewContext, TextWriter writer)
{
writer.Write("TEST");
}
}
Still working, but no more (master page / layout), just the plain TEST. So what's wrong doctor ?
EDIT : I feel like I need to be more specific on my objective. I have about ten cshtml pages that I have to copy among all my MVC projects because they are used by all of them. These pages are filled with just one method call using HtmlHelper extension like :
@Html.MvcSharedMenu()
In these extension methods I work with MvcHtmlStrings to build my html, I guess I could call these my "Custom Controls". I know about Razor Generator, but I'd like to avoid that solution especially when all the code is already outside of the View.