如何检查ASP.NET Core MVC的共享文件夹中是否存在视图? [英] How to check if a view exist in shared folder in ASP.NET Core MVC?
本文介绍了如何检查ASP.NET Core MVC的共享文件夹中是否存在视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何知道ASP.NET Core中是否存在视图?
How to know view exists in ASP.NET Core?
我正在搜索这样的伪代码:
I'm searching for a pseudo-code like this:
@if (Exists("/Views/Shared/SomeView.cshtml"))
{
Html.Partial("/Views/Shared/SomeView.cshtml"))
}
推荐答案
You can use FindView for this. Inside of a view, you can use dependency injection to get an instance of ICompositeViewEngine
, which is registered for you when adding the MVC services. Using this instance, it's possible to determine whether or not the view exists using something like the following:
@inject ICompositeViewEngine Engine
@if (Engine.FindView(ViewContext, "SomeView", isMainPage: false).Success)
{
@Html.Partial("SomeView");
}
如果您特别想检查视图是否在特定的文件夹中(例如,在您的示例中为共享"),则可以使用GetView
:
If you specifically want to check whether or not the View exists in a specific folder (e.g. Shared in your example), you can use GetView
:
@if (Engine.GetView(null, "Views/Shared/SomeView.cshtml", isMainPage: false).Success)
这篇关于如何检查ASP.NET Core MVC的共享文件夹中是否存在视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文