2

我正在尝试使用路径检查是否存在几个视图。但是,即使它们确实存在,也无法找到这些视图。

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;
}

以及我如何尝试寻找观点:

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

注意"~/Shared/Error""Error"是相同的视图,但只找到后者。

4

1 回答 1

7

当您使用路径时,您还需要指定扩展名:

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

当您不指定路径时,您不需要扩展,因为在这种情况下,视图引擎遵循标准约定来发现视图。

于 2011-05-17T11:14:15.047 回答