Convert a view to a string in MVC

638
Views
2
Answers

How to convert/ parse a view or Partial view rendered in to a string in c# .net MVC??

edit | flag
William Patrick
Asked on: Jun 16, 2011 at 5:24AM

2 Answers

9
1
9

You can try this code to render your view into a string

StringWriter sw = new StringWriter();
       
Server.Execute("PageToConvert.aspx", sw);
       
string htmlCodeToConvert = sw.GetStringBuilder().ToString();
edit | flag
Gokul A
Answered on: Jun 16, 2011 at 7:56PM
10
0
10

If you are capturing Controller Action into a string then try this..

public static string CaptureActionHtml(this Controller controller, TController targetController, string masterPageName, Func  action) where TController : Controller

       
{
   
if (controller == null)
   
{
   
throw new ArgumentNullException("controller");
   
}
   
if (targetController == null)
   
{
   
throw new ArgumentNullException("targetController");
   
}
   
if (action == null)
   
{
   
throw new ArgumentNullException("action");
   
}
   
// pass the current controller context to orderController
   
var controllerContext = controller.ControllerContext;
    targetController
.ControllerContext = controllerContext;

   
// replace the current context with a new context that writes to a string writer
   
var existingContext = HttpContext.Current;
   
var writer = new StringWriter();
   
var response = new HttpResponse(writer);
   
var context = new HttpContext(existingContext.Request, response) { User = existingContext.User };
   
HttpContext.Current = context;

   
// execute the action
   
var viewResult = action(targetController);

   
// change the master page name
   
if (masterPageName != null)
   
{
    viewResult
.MasterName = masterPageName;
   
}

   
// we have to set the controller route value to the name of the controller we want to execute
   
// because the ViewLocator class uses this to find the correct view
   
var oldController = controllerContext.RouteData.Values["controller"];
    controllerContext
.RouteData.Values["controller"] = typeof(TController).Name.Replace("Controller", "");

   
// execute the result
    viewResult
.ExecuteResult(controllerContext);

   
StringWriter sw = new StringWriter();
   
var xx = targetController.TempData["pdf"];
   
//var viewContext = new ViewContext(controllerContext, viewResult.View, new ViewDataDictionary(targetController.ViewData.Model), new TempDataDictionary(), sw);
   
var viewContext = new ViewContext(controllerContext, viewResult.View, viewResult.ViewData, new TempDataDictionary(), sw);
    viewResult
.View.Render(viewContext, HttpContext.Current.Response.Output);
    response
.Flush();

   
// restore the old route data
    controllerContext
.RouteData.Values["controller"] = oldController;

   
// restore the old context
   
HttpContext.Current = existingContext;

   
return sw.ToString();
   
}
edit | flag
Gokul A
Answered on: Jun 16, 2011 at 7:58PM

Post your Answer

Search

Welcome to Ask Amoeba!
This is 100% free and interactive site for sharing professional Questions and Answers, Opensource projects, Interview questions.
Learners, beginners, Experts stop and share your knowledge and ideas!

Browse Categories

Browse Tags