How to Determine if a View Exists

Topics: Customizing Orchard
Oct 21, 2014 at 5:44 PM
Edited Oct 21, 2014 at 6:04 PM
I'm trying to determine if a view exists or not so my controller can return a "default" view if the specified view is not found, but I'm having trouble implementing this in Orchard.

This is what I've tried so far but haven't been able to get working:

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

I have null as the value for the Master View, but don't know if that is correct. I can't figure out what the Master Views are???
Developer
Oct 21, 2014 at 11:00 PM
Edited Oct 21, 2014 at 11:01 PM
What specifically do you mean by "not working"? Is it not finding the specified view name? What is the value of "name" and how does your folder structure look like?
ShapeTemplateBindingStrategy uses the ILayoutAwareViewengine which you could inject, so you can find a view like this:
ILayoutAwareViewEngine _viewEngine; // Injected via constructor for example.
_viewEngine.FindView(ControllerContext, pathToView, useCache: true or false);
Oct 22, 2014 at 4:10 PM
Edited Oct 22, 2014 at 4:50 PM
My code never returns true meaning the result.View is coming back as null. The view I am looking for is located in a theme in Views/Orchard.Speedbump/Speedbump. I was using "Test Tenant Site Valid URL" as the name for my view "Test Tenant Site Valid URL.cshtml" I was not including any kind of path though. Could this be the problem?
Thank you.

Edit: Also when trying to use either of these methods "masterView" is expected as a parameter. What is "masterView" referring to?

Edit: I tried the following but it always returns true even when the view doesn't actually exist

private bool ViewExists(string name)
    {
        ViewEngineResult result = _viewEngine.FindPartialView(ControllerContext, "~Themes/TheThemeMachine/Views/Orchard.Speedbump/SpeedBump/" + name, useCache: false);
        return (result != null);
    }
where name is the name of the view I am looking for such as "Test InvalidURL" for the view "Test InvalidURL.cshtml"

Edit: If I use "return (result.View != null);" then it always returns false even when the view exists
Oct 22, 2014 at 5:10 PM
Edited Oct 22, 2014 at 5:15 PM
Am I using the wrong ControllerContext? I didn't set it up or anything I just typed in ControllerContext and it didn't give me any erros.

Edit: Also I'm having a hard time debugging as everytime I try to run the debugger in visual studio with google chrome it opens a different iis site with orchard instead of the one that I have in the code.
Oct 22, 2014 at 6:59 PM
I was able to get it working finaly with the following code:

ILayoutAwareViewEngine _viewEngine; // Injected via constructor for example.
private bool ViewExists(string name)
    {
        var result = _viewEngine.FindView(ControllerContext, name, null, useCache: false);
        return (result.View != null);
    }
Marked as answer by emeraldarcher on 10/22/2014 at 10:59 AM