Are you saying that you want some program to be able to leverage a View without being in a controller context at all, or are you saying that you want to be able to render a view into a string from within a controller, without calling some other controller?
For the former, I can't be of much assistance, but for the latter, we have this method in the base controller type that we inherit with all our other controllers:
/// <summary>
/// Generates a string based on the given PartialViewResult.
/// </summary>
/// <param name="partialViewResult"></param>
/// <returns></returns>
protected internal string RenderPartialViewToString(ViewResultBase partialViewResult)
{
Require.ThatArgument(partialViewResult != null);
var context = ControllerContext;
Require.That(context != null);
using (var sw = new StringWriter())
{
if (string.IsNullOrEmpty(partialViewResult.ViewName))
{
partialViewResult.ViewName = context.RouteData.GetRequiredString("action");
}
ViewEngineResult result;
if (partialViewResult.View == null)
{
result = partialViewResult.ViewEngineCollection.FindPartialView(context, partialViewResult.ViewName);
Require.That(result.View != null,
() => new InvalidOperationException(
"Unable to find view. Searched in: " +
string.Join(",", result.SearchedLocations)));
partialViewResult.View = result.View;
}
var view = partialViewResult.View;
var viewContext = new ViewContext(context, view, partialViewResult.ViewData,
partialViewResult.TempData, sw);
view.Render(viewContext, sw);
return sw.ToString();
}
}
Usage:
public ActionResult MyAction(...)
{
var myModel = GetMyModel(...);
string viewString = RenderPartialViewToString(PartialView("MyView", myModel));
// do something with the string
return someAction;
}
We actually use this in an event-based AJAX model, where most of our actions actually just return an AJAX-encoded list of client-side events, and some of those client-side events may be to update a particular DOM element with the string produces by rendering this partial view.