• <i id='lmsXU'><tr id='lmsXU'><dt id='lmsXU'><q id='lmsXU'><span id='lmsXU'><b id='lmsXU'><form id='lmsXU'><ins id='lmsXU'></ins><ul id='lmsXU'></ul><sub id='lmsXU'></sub></form><legend id='lmsXU'></legend><bdo id='lmsXU'><pre id='lmsXU'><center id='lmsXU'></center></pre></bdo></b><th id='lmsXU'></th></span></q></dt></tr></i><div id='lmsXU'><tfoot id='lmsXU'></tfoot><dl id='lmsXU'><fieldset id='lmsXU'></fieldset></dl></div>
  • <tfoot id='lmsXU'></tfoot>
    1. <small id='lmsXU'></small><noframes id='lmsXU'>

        <bdo id='lmsXU'></bdo><ul id='lmsXU'></ul>

        <legend id='lmsXU'><style id='lmsXU'><dir id='lmsXU'><q id='lmsXU'></q></dir></style></legend>

        MVC 6 Controller 中的 ControllerContext 和 ViewEngines 属性在哪里?

        Where are the ControllerContext and ViewEngines properties in MVC 6 Controller?(MVC 6 Controller 中的 ControllerContext 和 ViewEngines 属性在哪里?)

          <tbody id='P4jBJ'></tbody>

              1. <small id='P4jBJ'></small><noframes id='P4jBJ'>

              2. <legend id='P4jBJ'><style id='P4jBJ'><dir id='P4jBJ'><q id='P4jBJ'></q></dir></style></legend>

                <tfoot id='P4jBJ'></tfoot>
                  <bdo id='P4jBJ'></bdo><ul id='P4jBJ'></ul>
                • <i id='P4jBJ'><tr id='P4jBJ'><dt id='P4jBJ'><q id='P4jBJ'><span id='P4jBJ'><b id='P4jBJ'><form id='P4jBJ'><ins id='P4jBJ'></ins><ul id='P4jBJ'></ul><sub id='P4jBJ'></sub></form><legend id='P4jBJ'></legend><bdo id='P4jBJ'><pre id='P4jBJ'><center id='P4jBJ'></center></pre></bdo></b><th id='P4jBJ'></th></span></q></dt></tr></i><div id='P4jBJ'><tfoot id='P4jBJ'></tfoot><dl id='P4jBJ'><fieldset id='P4jBJ'></fieldset></dl></div>
                • 本文介绍了MVC 6 Controller 中的 ControllerContext 和 ViewEngines 属性在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我创建了一个新的 MVC6 项目并构建了一个新站点.目标是获得视图的渲染结果.我找到了以下代码,但我无法让它工作,因为我找不到 ControllerContextViewEngines.

                  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.

                  这是我要重写的代码:

                  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();
                      }
                  }
                  

                  推荐答案

                  更新:我正在更新它以使用 .Net Core 2.x,因为自 2015 年以来 API 发生了变化!

                  Update: I'm updating this to work with .Net Core 2.x as the APIs have changed since 2015!

                  首先,我们可以利用 ASP.Net MVC Core 附带的内置依赖注入,这将为我们提供手动渲染视图所需的 ICompositeViewEngine 对象.例如,一个控制器看起来像这样:

                  First of all we can leverage the built in dependency injection that comes with ASP.Net MVC Core which will give us the ICompositeViewEngine object we need to render our views manually. So for example, a controller would look like this:

                  public class MyController : Controller
                  {
                      private ICompositeViewEngine _viewEngine;
                  
                      public MyController(ICompositeViewEngine viewEngine)
                      {
                          _viewEngine = viewEngine;
                      }
                  
                      //Rest of the controller code here
                  }
                  

                  接下来,我们实际需要渲染视图的代码.请注意,它现在是一个 async 方法,因为我们将在内部进行异步调用:

                  Next, the code we actually need to render a view. Note that is is now an async method as we will be making asynchronous calls internally:

                  private async Task<string> RenderPartialViewToString(string viewName, object model)
                  {
                      if (string.IsNullOrEmpty(viewName))
                          viewName = ControllerContext.ActionDescriptor.ActionName;
                  
                      ViewData.Model = model;
                  
                      using (var writer = new StringWriter())
                      {
                          ViewEngineResult viewResult = 
                              _viewEngine.FindView(ControllerContext, viewName, false);
                  
                          ViewContext viewContext = new ViewContext(
                              ControllerContext, 
                              viewResult.View, 
                              ViewData, 
                              TempData, 
                              writer, 
                              new HtmlHelperOptions()
                          );
                  
                          await viewResult.View.RenderAsync(viewContext);
                  
                          return writer.GetStringBuilder().ToString();
                      }
                  }
                  

                  要调用方法,就这么简单:

                  And to call the method, it's as simple as this:

                  public async Task<IActionResult> Index()
                  {
                      var model = new TestModel
                      {
                          SomeProperty = "whatever"
                      }
                  
                      var renderedView = await RenderPartialViewToString("NameOfView", model);
                  
                      //Do what you want with the renderedView here
                  
                      return View();
                  }
                  

                  这篇关于MVC 6 Controller 中的 ControllerContext 和 ViewEngines 属性在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to keep the Text of a Read only TextBox after PostBack()?(PostBack()之后如何保留只读文本框的文本?)
                  Winforms Textbox - Using Ctrl-Backspace to Delete Whole Word(Winforms 文本框 - 使用 Ctrl-Backspace 删除整个单词)
                  C# - Add button click events using code(C# - 使用代码添加按钮单击事件)
                  Multi-color TextBox C#(多色文本框 C#)
                  How can i set the caret position to a specific index in passwordbox in WPF(如何将插入符号位置设置为 WPF 密码框中的特定索引)
                  C# Numeric Only TextBox Control(C# 纯数字文本框控件)

                    <bdo id='HOIP8'></bdo><ul id='HOIP8'></ul>
                      <tfoot id='HOIP8'></tfoot>

                        <legend id='HOIP8'><style id='HOIP8'><dir id='HOIP8'><q id='HOIP8'></q></dir></style></legend>
                          <tbody id='HOIP8'></tbody>
                      • <small id='HOIP8'></small><noframes id='HOIP8'>

                            <i id='HOIP8'><tr id='HOIP8'><dt id='HOIP8'><q id='HOIP8'><span id='HOIP8'><b id='HOIP8'><form id='HOIP8'><ins id='HOIP8'></ins><ul id='HOIP8'></ul><sub id='HOIP8'></sub></form><legend id='HOIP8'></legend><bdo id='HOIP8'><pre id='HOIP8'><center id='HOIP8'></center></pre></bdo></b><th id='HOIP8'></th></span></q></dt></tr></i><div id='HOIP8'><tfoot id='HOIP8'></tfoot><dl id='HOIP8'><fieldset id='HOIP8'></fieldset></dl></div>