The layout page "~ / Views / Shared /" was not found in the following path
Here's a new twist on this old mistake. Many of my pages use the layout page, so they sit at the top
@{
Layout = "~/Views/Shared/" + ViewBag.layout;
}
If the widget layout is set to action filter applied to the controller as such
namespace somenamespace.Controllers {
[SessionSettings]
public class MyController : Controller {
where the action filter does this
public class SessionSettings : ActionFilterAttribute {
public override void OnActionExecuting(ActionExecutingContext filterContext) {
dynamic viewBag = filterContext.Controller.ViewBag;
string layout = some database lookup
if (layout == null) layout = "_defaultLayout.cshtml";
viewBag.layout = layout
and it works really well most of the time. But when I check the event logs - applications - I see warnings, event ID 1309, event ID 3005, "An unhandled exception occurred" "Layout page" ~ / Views / Shared / "was not found in the following path:" ~ / Views / Shared / "."
And here the kicker, often the event happens on pages that DO NOT use layouts, they have it at the top
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>...
Anyone have any thoughts on this? thank
source to share
I traced back to the source and found that the issue was with the installation Layout = "~/Views/Shared/" + ViewBag.layout;
When this property is set, the method is called WebPageExecutingBase.NormalizeLayoutPagePath(string path)
.
This creates an absolute / relative path and then checks if a file with that name exists. Since ViewBag.layout
it is null this fails and therefore errors (you can see the source of this method on codeplex , about half way down.
It doesn't matter what you installed later Layout = null
, the code is called first _ViewStart
, so an error occurs.
Your best option I see is to just check if the value ViewBag.layout
is null before setting it:
if(ViewBag.layout != null)
{
Layout = "~/Views/Shared/" + ViewBag.layout;
}
I think so, you also don't need to explicitly point it to null
if they don't have a layout.
source to share