[ACCEPTED]-Does a view exist in ASP.NET MVC?-asp.net-mvc

Accepted answer
Score: 158
 private bool ViewExists(string name)
 {
     ViewEngineResult result = ViewEngines.Engines.FindView(ControllerContext, name, null);
     return (result.View != null);
 }

For those looking for a copy/paste extension 1 method:

public static class ControllerExtensions
{
    public static bool ViewExists(this Controller controller, string name)
    {
        ViewEngineResult result = ViewEngines.Engines.FindView(controller.ControllerContext, name, null);
        return (result.View != null);
    }
}
Score: 20

What about trying something like the following 1 assuming you are using only one view engine:

bool viewExists = ViewEngines.Engines[0].FindView(ControllerContext, "ViewName", "MasterName", false) != null;
Score: 8

Here's another [not necessarily recommended] way 1 of doing it

 try
 {
     @Html.Partial("Category/SearchPanel/" + Model.CategoryKey)
 }
 catch (InvalidOperationException) { }
Score: 3

If you want to re-use this across multiple 4 controller actions, building on the solution 3 given by Dave, you can define a custom view 2 result as follows:

public class CustomViewResult : ViewResult
{
    protected override ViewEngineResult FindView(ControllerContext context)
    {
        string name = SomeMethodToGetViewName();

        ViewEngineResult result = ViewEngines.Engines.FindView(context, name, null);

        if (result.View != null)
        {
            return result;
        }

        return base.FindView(context);
    }

    ...
}

Then in your action simply 1 return an instance of your custom view:

public ActionResult Index()
{ 
    return new CustomViewResult();
}
Score: 3

In asp.net core 2.x the ViewEngines property no longer 7 exists so we have to use the ICompositeViewEngine service. This 6 a variant of the accepted answer using dependency 5 injection:

public class DemoController : Controller
{
    private readonly IViewEngine _viewEngine;

    public DemoController(ICompositeViewEngine viewEngine)
    {
        _viewEngine = viewEngine;
    }

    private bool ViewExists(string name)
    {
        ViewEngineResult viewEngineResult = _viewEngine.FindView(ControllerContext, name, true);
        return viewEngineResult?.View != null;
    }

    public ActionResult Index() ...
}

For the curious: The base interface 4 IViewEngine is not registered as a service so we must 3 inject ICompositeViewEngine instead. The FindView() method however is 2 provided by IViewEngine so the member variable may 1 use the base interface.

Score: 1
ViewEngines.Engines.FindView(ViewContext.Controller.ControllerContext, "View Name").View != null

My 2 cents.

0

Score: 0

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

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

More Related questions