Reputation: 874
I have the following _Layout.cshtml:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>My Title</title>
</head>
<body>
<div class="container body-content">
@await Html.PartialAsync("~/Views/" + Model.Theme + "/_Header.cshtml")
@RenderBody()
@await Html.PartialAsync("~/Views/" + Model.Theme + "/_Footer.cshtml")
</div>
</body>
</html>
Depending on the value of "Model.Theme", I want to render that partial if it exist. However, if it doesn't exist, then I don't want that partial to render.
How can I prevent "InvalidOperationException" if the Partial doesn't exist?
Please note that I want to test for the partial, not the Model.Theme. The theme will always exist but not all partials will be available.
Upvotes: 0
Views: 83
Reputation: 5806
fallback to default
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>My Title</title>
</head>
<body>
<div class="container body-content">
@if (System.IO.File.Exists(@"Views/" + Model.Name + "/_Header.cshtml"))
{
@await Html.PartialAsync("~/Views/" + Model.Name + "/_Header.cshtml")
}
@RenderBody()
@if (System.IO.File.Exists(@"Views/" + Model.Name + "/_Footer.cshtml"))
{
@await Html.PartialAsync("~/Views/" + Model.Name + "/_Footer.cshtml")
}
</div>
</body>
</html>
Upvotes: 1