Resolver或ServiceProvider使用ICompositeViewEngine

时间:2016-08-03 17:06:22

标签: c# .net asp.net-core asp.net-core-mvc

我正在尝试在ASP.NET Core MVC中使用ICompositeViewEngine替换System.Web.Mvc中的ViewEngine,因为它在.NET Core中不再可用。我通常尝试在此项目中将webform从ASP.NET迁移到ASP.NET Core。

我找到了以下解决方案:Where are the ControllerContext and ViewEngines properties in MVC 6 Controller?我相信这可以解决我的问题。我还在github问题中找到了与ServiceProvider类似的引擎创建:https://github.com/aspnet/Mvc/issues/3091

但是,我不确定我可能会缺少哪些依赖项或框架,因为我是.NET的新手。我有以下命名空间:

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewEngines;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.Extensions.DependencyInjection;

我认为可能与我的问题有关。

我的原始代码是:

    public static string RenderPartialToString(Controller controller, string viewName, object model)
    {
        controller.ViewData.Model = model;

        using (StringWriter sw = new StringWriter())
        {
            ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
            ViewContext viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
            viewResult.View.Render(viewContext, sw);

            return "document.write('" + sw.GetStringBuilder().Replace('\n', ' ').Replace('\r', ' ').Replace("'","\\'").ToString() + "');";
        }
    }

现在我正在尝试使用以下任一方法:

 var engine = Resolver.GetService(typeof(ICompositeViewEngine)) as ICompositeViewEngine;
 var engine2 = IServiceProvider.GetService(typeof(ICompositeViewEngine)) as ICompositeViewEngine;

我是否正确地解决这个问题?有没有更简单的方法来替换.NET Core中的System.Web.Mvc ViewEngines?我需要修复什么" 在当前上下文中不存在"解析器和/或ServiceProvider的错误?

感谢。我希望我能够遵循问题指南。

编辑:如果我的代码中包含此问题的任何其他内容,请告诉我。我目前正在阅读依赖注入以更好地了解情况。

1 个答案:

答案 0 :(得分:5)

你大部分时间都在正确的轨道上。 ASP.NET Core摆脱了许多静态对象,因此您无法执行Resolver.GetService之类的操作。 Resolver不存在。相反,请使用dependency injection系统。

如果您只需要从控制器访问ICompositeViewEngine,请将其注入构造函数:

public MyController(ICompositeViewEngine viewEngine)
{
    // save a reference to viewEngine
}

如果您想要一个处理Razor到字符串渲染的离散服务,您需要在启动时注册它:

public void ConfigureServices(IServiceCollection services)
{
    // (Other code...)

    services.AddTransient<IViewRenderingService, ViewRenderingService>();

    services.AddMvc();
}

服务本身看起来像这样:

using System;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewEngines;
using Microsoft.AspNetCore.Mvc.ViewFeatures;

public interface IViewRenderingService
{
    string RenderPartialView(ActionContext context, string name, object model = null);
}

public class ViewRenderingService : IViewRenderingService
{
    private readonly ICompositeViewEngine _viewEngine;
    private readonly ITempDataProvider _tempDataProvider;

    public ViewRenderingService(ICompositeViewEngine viewEngine, ITempDataProvider tempDataProvider)
    {
        _viewEngine = viewEngine;
        _tempDataProvider = tempDataProvider;
    }

    public string RenderPartialView(ActionContext context, string name, object model)
    {
        var viewEngineResult = _viewEngine.FindView(context, name, false);

        if (!viewEngineResult.Success)
        {
            throw new InvalidOperationException(string.Format("Couldn't find view '{0}'", name));
        }

        var view = viewEngineResult.View;

        using (var output = new StringWriter())
        {
            var viewContext = new ViewContext(
                context,
                view,
                new ViewDataDictionary(
                    new EmptyModelMetadataProvider(),
                    new ModelStateDictionary())
                {
                    Model = model
                },
                new TempDataDictionary(
                    context.HttpContext,
                    _tempDataProvider),
                output,
                new HtmlHelperOptions());

            view.RenderAsync(viewContext).GetAwaiter().GetResult();

            return output.ToString();
        }
    }
}

要从控制器中使用它,请注入并调用它:

public class HomeController : Controller
{
    private readonly IViewRenderingService _viewRenderingService;

    public HomeController(IViewRenderingService viewRenderingService)
    {
        _viewRenderingService = viewRenderingService;
    }

    public IActionResult Index()
    {
        var result = _viewRenderingService.RenderPartialView(ControllerContext, "PartialViewName", model: null);
        // do something with the string

        return View();
    }
}

如果您想完全在MVC之外使用Razor,请参阅this answer