Required dependencies for Resolver or ServiceProvider to use ICompositeViewEngine

I am trying to use ICompositeViewEngine in ASP.NET Core MVC to replace ViewEngine from System.Web.Mvc as it is no longer available in .NET Core. I usually try to migrate a web form from ASP.NET to ASP.NET Core in this project.

I found the following solution: Where are the ControllerContext and ViewEngines properties in MVC 6 Controller? , and I believe that this can solve my problem. I also found a similar engine creation with ServiceProvider in the github question: https://github.com/aspnet/Mvc/issues/3091

However, I'm not sure which dependencies or frameworks I can lose, as I am very new to .NET. I have the following namespaces:

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

I believe this may be due to my problem.

My source code:

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

And now I'm trying to use one of the following:

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

Am I on the right track to fix this? Is there an easier way to replace System.Web.Mvc ViewEngines in .NET Core? What do I need to fix " does not exist in the current context " for Resolver and / or ServiceProvider?

Thank. I hope I was able to follow the recommendations on the issues.

Edit: Please let me know if I should include anything else from my code for this question. I am currently reading about Injection Dependency to better understand the situation.

+2
source share
1 answer

. ASP.NET Core , , Resolver.GetService. Resolver . .

ICompositeViewEngine , :

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

, Razor-to-string, :

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

Razor MVC, . .

+4

Source: https://habr.com/ru/post/1650573/


All Articles