简体   繁体   English

如何检查ASP.NET Core MVC的共享文件夹中是否存在视图?

[英]How to check if a view exist in shared folder in ASP.NET Core MVC?

How to know view exists in ASP.NET Core? 如何知道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. 您可以为此使用FindView 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. 在视图内部,可以使用依赖注入来获取ICompositeViewEngine的实例,该实例在添加MVC服务时已为您注册。 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");
}

If you specifically want to check whether or not the View exists in a specific folder (eg Shared in your example), you can use GetView : 如果您特别想检查视图是否在特定文件夹中(例如,示例中为“共享”),则可以使用GetView

@if (Engine.GetView(null, "Views/Shared/SomeView.cshtml", isMainPage: false).Success)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法选中 ASP.NET Core MVC 视图中的复选框 - Cannot check checkbox in ASP.NET Core MVC view 如何使用 controller 或在 ASP.NET Core MVC 中查看 wwwroot 文件夹中的 index.html 文件 - How to render index.html file that is in wwwroot folder using controller or view in ASP.NET Core MVC 如何检查ASP.NET MVC 4中是否存在文件 - How to check if file exist in ASP.NET MVC 4 Asp.Net Mvc - 如何在共享视图中拥有“控制器” - Asp.Net Mvc - How to have a “controller” in shared view 在ASP.NET MVC 4中使用共享视图 - Using shared view in ASP.NET MVC 4 如何从 ASP.NET Core MVC 中的视图创建 object - How to create an object from the view in ASP.NET Core MVC 如何将视图重定向到主页 - ASP.NET Core MVC - How to redirect view to homepage - ASP.NET Core MVC 如何在 Asp.Net Core MVC 的 ExceptionFilter 中返回带有 model 的视图 - How return View with model in the ExceptionFilter in Asp.Net Core MVC Asp.Net Core 2.2身份页面不适用于View / Shared文件夹中的Layout - Asp.Net Core 2.2 Identity pages wont work with Layout in View/Shared folder 部分视图位于共享文件夹中时,ASP.NET MVC“@model dynamic”无法识别模型属性 - ASP.NET MVC '@model dynamic' not recognizing Model properties when partial view is in Shared folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM