如何在 asp.net 中将布局呈现为字符串?
how to render layout as string in asp.net?
我将整个布局作为数据库中的一个字符串,我想将布局从数据库呈现到我的视图。
例如:
public ActionResult index()
{
string layout = GetLayout();
//in below how should I render layout to view
return View("index", layout);
}
我已经使用 result = Engine.Razor.RunCompile(template, key.ToString());
渲染布局,但它避免渲染 HTML 助手。
似乎没有内置机制可以实现 one.Needs 为该 one.This 主题提供自定义解决方案已在此 post 中讨论过。
Dynamically Produce Razor Views at Runtime? 你可以参考那个。
我正在使用下面的代码并且它工作正常 me.i 我也在 Web API 中使用它以字符串形式返回视图。
要了解更多信息,您可以创建 Static Class 并将以下方法设为静态。
public string RenderPartialView(string controllerName, string viewName, object model = null) {
System.Web.HttpContextBase contextBase = new System.Web.HttpContextWrapper(System.Web.HttpContext.Current);
var routeData = new RouteData();
routeData.Values.Add("controller", controllerName);
var controllerContext = new System.Web.Mvc.ControllerContext(contextBase, routeData,
new EmptyController());
var razorViewEngine = new System.Web.Mvc.RazorViewEngine();
var razorViewResult = razorViewEngine.FindPartialView(controllerContext, viewName, false);
var writer = new StringWriter();
System.Web.Mvc.ViewContext viewContext;
viewContext = new System.Web.Mvc.ViewContext(controllerContext, razorViewResult.View,
new System.Web.Mvc.ViewDataDictionary(model), new System.Web.Mvc.TempDataDictionary(), writer);
viewContext.ViewData["controller"] = controllerName;
HttpContext.Current.Items.Add("controller", controllerName);
razorViewResult.View.Render(viewContext, writer);
string htmlString = writer.ToString();
writer.Dispose();
return htmlString;
}
private class EmptyController: System.Web.Mvc.ControllerBase {
protected override void ExecuteCore() {}
}
现在请找到我们如何从控制器调用它的示例。
RenderPartialView("Home", string.Format("~/Views/Home/{0}.cshtml", "Index"));
希望对你有用。
我将整个布局作为数据库中的一个字符串,我想将布局从数据库呈现到我的视图。 例如:
public ActionResult index()
{
string layout = GetLayout();
//in below how should I render layout to view
return View("index", layout);
}
我已经使用 result = Engine.Razor.RunCompile(template, key.ToString());
渲染布局,但它避免渲染 HTML 助手。
似乎没有内置机制可以实现 one.Needs 为该 one.This 主题提供自定义解决方案已在此 post 中讨论过。 Dynamically Produce Razor Views at Runtime? 你可以参考那个。
我正在使用下面的代码并且它工作正常 me.i 我也在 Web API 中使用它以字符串形式返回视图。
要了解更多信息,您可以创建 Static Class 并将以下方法设为静态。
public string RenderPartialView(string controllerName, string viewName, object model = null) {
System.Web.HttpContextBase contextBase = new System.Web.HttpContextWrapper(System.Web.HttpContext.Current);
var routeData = new RouteData();
routeData.Values.Add("controller", controllerName);
var controllerContext = new System.Web.Mvc.ControllerContext(contextBase, routeData,
new EmptyController());
var razorViewEngine = new System.Web.Mvc.RazorViewEngine();
var razorViewResult = razorViewEngine.FindPartialView(controllerContext, viewName, false);
var writer = new StringWriter();
System.Web.Mvc.ViewContext viewContext;
viewContext = new System.Web.Mvc.ViewContext(controllerContext, razorViewResult.View,
new System.Web.Mvc.ViewDataDictionary(model), new System.Web.Mvc.TempDataDictionary(), writer);
viewContext.ViewData["controller"] = controllerName;
HttpContext.Current.Items.Add("controller", controllerName);
razorViewResult.View.Render(viewContext, writer);
string htmlString = writer.ToString();
writer.Dispose();
return htmlString;
}
private class EmptyController: System.Web.Mvc.ControllerBase {
protected override void ExecuteCore() {}
}
现在请找到我们如何从控制器调用它的示例。
RenderPartialView("Home", string.Format("~/Views/Home/{0}.cshtml", "Index"));
希望对你有用。