凡在6 MVC的控制器和ControllerContext性能ViewEngines? [英] Where are the ControllerContext and ViewEngines properties in MVC 6 Controller?

查看:190
本文介绍了凡在6 MVC的控制器和ControllerContext性能ViewEngines?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个新的项目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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆