凡在6 MVC的控制器和ControllerContext性能ViewEngines? [英] Where are the ControllerContext and ViewEngines properties in MVC 6 Controller?
问题描述
我已经创建了一个新的项目MVC6,建设一个新的网站。我们的目标是获得一个视图的渲染结果。我发现下面的code,但我不能让它工作,因为我无法找到 ControllerContext
和 ViewEngines
。
下面是code我想重写:
保护字符串RenderPartialViewToString(字符串的viewName,对象模型)
{
如果(string.IsNullOrEmpty(的viewName))
的viewName = ControllerContext.RouteData.GetRequiredString(行动); ViewData.Model =模型; 使用(StringWriter的SW =新的StringWriter())
{
ViewEngineResult的ViewResult = ViewEngines.Engines.FindPartialView(ControllerContext,的viewName);
ViewContext viewContext =新ViewContext(ControllerContext,viewResult.View,ViewData的,TempData的,SW);
viewResult.View.Render(viewContext,SW); 返回sw.GetStringBuilder()的ToString()。
}
}
ControllerContext
首先,我要指出一个控制器已不再是一个顶级的背景下,一个动作。所以,你可以替换大多 ControllerContext
与 ActionContext中
ViewEngines
这是事情变得有点尴尬!我不完全知道如何获得defaut视图引擎,但新的DI框架增加了一个参考 CompositeViewEngine
它可以为你做这项工作。例如:
VAR引擎= Resolver.GetService(typeof运算(ICompositeViewEngine))作为ICompositeViewEngine;
所以把这个放在一起,这应该为你工作:
保护字符串RenderPartialViewToString(字符串的viewName,对象模型)
{
如果(string.IsNullOrEmpty(的viewName))
的viewName = ActionContext.ActionDescriptor.Name; ViewData.Model =模型; 使用(StringWriter的SW =新的StringWriter())
{
VAR引擎= Resolver.GetService(typeof运算(ICompositeViewEngine))
作为ICompositeViewEngine;
ViewEngineResult的ViewResult = engine.FindPartialView(ActionContext中,的viewName); ViewContext viewContext =新ViewContext(
ActionContext中,
viewResult.View,
ViewData的,
TempData的,
SW,
新HtmlHelperOptions()//添加在此参数
); //一切都是异步吧!
VAR T = viewResult.View.RenderAsync(viewContext);
t.Wait(); 返回sw.GetStringBuilder()的ToString()。
}
}
I've created a new MVC6 project and building a new site. The goal is to get the rendered result of a view. I found the following code, but I can't get it to work because I can't find the ControllerContext
and the ViewEngines
.
Here is the code I want to rewrite:
protected string RenderPartialViewToString(string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = ControllerContext.RouteData.GetRequiredString("action");
ViewData.Model = model;
using (StringWriter sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
ControllerContext
First I should mention that a controller is no longer a top level context, an action is. So you can mostly replace ControllerContext
with ActionContext
ViewEngines
This is where things get a bit awkward! I'm not entirely sure how to get the defaut view engine but the new DI framework has added a reference to CompositeViewEngine
which may do the job for you. For example:
var engine = Resolver.GetService(typeof(ICompositeViewEngine)) as ICompositeViewEngine;
So putting all this together, this should work for you:
protected string RenderPartialViewToString(string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = ActionContext.ActionDescriptor.Name;
ViewData.Model = model;
using (StringWriter sw = new StringWriter())
{
var engine = Resolver.GetService(typeof(ICompositeViewEngine))
as ICompositeViewEngine;
ViewEngineResult viewResult = engine.FindPartialView(ActionContext, viewName);
ViewContext viewContext = new ViewContext(
ActionContext,
viewResult.View,
ViewData,
TempData,
sw,
new HtmlHelperOptions() //Added this parameter in
);
//Everything is async now!
var t = viewResult.View.RenderAsync(viewContext);
t.Wait();
return sw.GetStringBuilder().ToString();
}
}
这篇关于凡在6 MVC的控制器和ControllerContext性能ViewEngines?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!