asp.net 核心中的 ViewEngineCollection 替代方案? [英] ViewEngineCollection alternative in asp.net core?

查看:31
本文介绍了asp.net 核心中的 ViewEngineCollection 替代方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个 Asp.Net MVC 应用程序迁移到 Asp.Net Core.

I am migrating an Asp.Net MVC application to Asp.Net Core.

我遇到了这行代码:

if (this.ViewEngineCollection.FindView(this.ControllerContext, viewPath, null).View != null)
{
    return this.View(viewPath);
}

我找不到在 asp.net 核心中执行 ViewEngineCollection.FindView 的任何替代方法.

I couldn't find any alternative way of performing ViewEngineCollection.FindView in asp.net core.

感谢任何帮助.

推荐答案

我得到了解决方案,感谢 Henk Mollema.

I got the solution, thanks to Henk Mollema.

  • 控制器类
public class NewsController : Controller
{
    private readonly IRazorViewEngine _razorViewEngine;
    private readonly IActionContextAccessor _actionContextAccessor;

    public NewsController(IRazorViewEngine razorViewEngine, IActionContextAccessor actionContextAccessor)
    {
        _razorViewEngine = razorViewEngine;
        _actionContextAccessor = actionContextAccessor;
    }

    public IActionResult Index()
    {   
        var gotView = _razorViewEngine.GetView(string.Empty, viewName, true);

        if (gotView.Success)   //gets true
        {
            return View(viewName);
        }
        return NotFound();
    }
}

* 启动.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
    services.AddMvc();
}

可以在 GitHub 此处上找到相同的内容.

The same can be found on GitHub here.

这篇关于asp.net 核心中的 ViewEngineCollection 替代方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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