Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The layout page could not be found

I get the error: The layout page "_Layout" could not be found at the following path: "~/Views/Home/_Layout".

But layout page is at this path: "~/Views/Shared/_Layout"

What can it be for problem?

I just started the project and it look like this:

Controller:

namespace Testing.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/

        public ActionResult Index()
        {
            return View();
        }
    }
}

Index view:

@model dynamic

@{
    ViewBag.Title = "title";
    Layout = "_Layout";
}

<h2>title</h2>

_ViewStart.cshtml:

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

Solution Explorer:

Solution Explorer:

like image 256
anleon Avatar asked Feb 17 '13 17:02

anleon


3 Answers

Make sure that in your ~/Views/_ViewStart.cshtml file you have set the correct path:

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

Also if in your views you are overriding the layout ensure that the proper path is specifid for this layout. And in addition to that there could be some server side code which is setting the layout (such as custom action filters, or the ViewResult overload which allows to specify a layout, ...).


UPDATE:

You seem to have set the Layout like this:

@{ 
    ViewBag.Title = "title"; 
    Layout = "_Layout"; 
} 

You need to specify the location to the layout as absolute path:

@{ 
    ViewBag.Title = "title"; 
    Layout = "~/Views/Shared/_Layout.cshtml";
} 

But an even better way is to get rid of this Layout setting in your Index view:

@{ 
    ViewBag.Title = "title"; 
}

Now the value from your _ViewStart.cshtml will be used.

like image 105
Darin Dimitrov Avatar answered Nov 03 '22 10:11

Darin Dimitrov


I had this same problem appear once i had deployed my service, but could not replicate locally. For me the issue was that the View had None set for its build action. So the error was correct in that the file did not exist on the server, despite it existing in the repository.

Solution to fix this:

  1. Right click on missing view and open Properties View Properties
  2. Ensure Build Action is set to Content
  3. Update csproj file to match new Build Action. .csproj file
like image 9
DjGimli Avatar answered Nov 03 '22 10:11

DjGimli


Using the wrong tilde can also produce this error.

Make sure you use the tilde "~" on your keyboard.

If you copied the code from a book, the tilde could be an invalid character (note that there are more than one tilde in Unicode).

like image 1
Gerhard Liebenberg Avatar answered Nov 03 '22 10:11

Gerhard Liebenberg