我正在将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 .
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(); } }
public void ConfigureServices(IServiceCollection services) { services.AddSingleton<IActionContextAccessor, ActionContextAccessor>(); services.AddMvc(); }
同样可以在GitHub here上找到 .
2 回答
您可以使用依赖注入注入
IRazorViewEngine
实例并调用FindView
. 您必须使用此接口传入ActionContext
实例而不是ControllerContext
.我得到了解决方案,感谢Henk Mollema .
同样可以在GitHub here上找到 .