2

I'm trying to check if a couple of views exist by using paths. But the views cannot be found even if they do exist.

private string SelectFirstView(ControllerContext ctx, params string[] viewNames)
{
    return viewNames.First(view => ViewExists(ctx, view));
}

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

And how I try to find the views:

var viewName = SelectFirstView(ctx, statusCodeName,
                               "~/Error/" + statusCodeName,
                               "~/Error/General",
                               "~/Shared/Error",
                               "Error");

Note that "~/Shared/Error" and "Error" is the same view, but only the latter is found.

jgauffin
  • 99,844
  • 45
  • 235
  • 372

1 Answers1

7

When you are working with paths you need to specify the extension as well:

~/Error/General.cshtml
~/Shared/Error.cshtml
...

When you don't specify a path you don't need the extension as in this case the view engine follows standard conventions to discover the views.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928