Does a view exist in ASP.NET MVC?

前端 未结 7 769
粉色の甜心
粉色の甜心 2020-11-28 09:59

Is it possible to determine if a specific view name exists from within a controller before rendering the view?

I have a requirement to dynamically determine the name

7条回答
  •  囚心锁ツ
    2020-11-28 10:56

    Here's how to do it in Razor for Core 2.2 etc. Note that the call is "GetView", not "Find View)

    @using Microsoft.AspNetCore.Mvc.ViewEngines
    @inject ICompositeViewEngine Engine
    ...
    @if (Engine.GetView(scriptName, scriptName, isMainPage: false).Success) 
    {
        @await Html.PartialAsync(scriptName)
    }
    

提交回复
热议问题