jgauffin
jgauffin

Reputation: 101150

Trying to use FindView with a path

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.

Upvotes: 2

Views: 3934

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038850

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.

Upvotes: 7

Related Questions