我正在将 Asp.Net MVC 应用程序迁移到 Asp.Net Core。
我遇到了这行代码:
if (this.ViewEngineCollection.FindView(this.ControllerContext, viewPath, null).View != null)
{
return this.View(viewPath);
}
我找不到在 asp.net 核心中执行 ViewEngineCollection.FindView 的任何替代方法。
感谢任何帮助。
我正在将 Asp.Net MVC 应用程序迁移到 Asp.Net Core。
我遇到了这行代码:
if (this.ViewEngineCollection.FindView(this.ControllerContext, viewPath, null).View != null)
{
return this.View(viewPath);
}
我找不到在 asp.net 核心中执行 ViewEngineCollection.FindView 的任何替代方法。
感谢任何帮助。
您可以IRazorViewEngine使用依赖注入注入实例并调用FindView. 您必须传入一个ActionContext实例而不是ControllerContext使用此接口。
感谢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 上找到相同的内容。