Where are the ControllerContext and ViewEngines properties in MVC 6 Controller?

后端 未结 4 1260
温柔的废话
温柔的废话 2020-11-28 09:21

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

4条回答
  •  有刺的猬
    2020-11-28 10:04

    The solution provided by Martin Tomes worked for me but I had to replace:

    ViewEngineResult viewResult = engine.FindView(ControllerContext, viewName, false);
    

    with

    ViewEngineResult viewResult = engine.GetView(_env.WebRootPath, viewName, false);
    

    Also in controller constructor had to add

    private IHostingEnvironment _env;
    
    public AccountController(IHostingEnvironment env)
    {
        _env = env;
    }
    

提交回复
热议问题