c# mvc 在控制器中动态解析cshtml文件并获取对应的html代码
2021-04-09 22:29
标签:out view 动态解析 text model str write row try 调用如下 c# mvc 在控制器中动态解析cshtml文件并获取对应的html代码 标签:out view 动态解析 text model str write row try 原文地址:https://www.cnblogs.com/q149072205/p/9056154.htmlpublic static string GetViewHtml(ControllerContext context, string viewName, Object param)
{
if (string.IsNullOrEmpty(viewName))
viewName = context.RouteData.GetRequiredString("action");
//context.Controller.ViewData.Model = param;
using (var sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(context, viewName);
var viewContext = new ViewContext(context,
viewResult.View,
context.Controller.ViewData,
context.Controller.TempData,
sw);
try
{
viewResult.View.Render(viewContext, sw);
}
catch (Exception ex)
{
throw;
}
return sw.GetStringBuilder().ToString();
}
}
public ActionResult A1(){
ViewData["a"]=1;
retrun View();
}
public ActionResult A2(){
ViewData["a"]=1;//这里也要有ViewData,不然报错
var html=GetViewHtml(this.ControllerContext, "A1", null);
}
文章标题:c# mvc 在控制器中动态解析cshtml文件并获取对应的html代码
文章链接:http://soscw.com/index.php/essay/73521.html