noviKorisnik
noviKorisnik

Reputation: 28

MVC 4 Intranet application, multiple layout

I am getting an error in developing a MVC4 Intranet application, trying to use multiple page layouts. As I see, the problem is not present when Internet application is used, here is quick demo.

Create new Internet application using Razor engine. Copy file _Layout.cshtml to Popup_Layout.cshtml (both in ~/Views/Shared), and strip header and footer sections in Popup_Layout. In ~/Controllers/HomeController.cs modify Index action with following code:

    public ActionResult Index()
    {
        ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

        string layout = "_Layout";

        if (DateTime.Now.Ticks % 2 == 1)
        {
            layout = "Popup_Layout";
        }

        return View("Index", layout);
    }

That creates a simple game which randomly chooses to use one of two available page layouts. When compiled and tested, with refresh in browser we can see start page with or without header and footer sections.

Now if we repeat the exactly same scenario with only difference that project is Intranet application - build goes OK, but with refreshes in browser instead of getting a page without header and footer we get the server error, with the top of Stack trace says:

    [InvalidOperationException: The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
    ~/Views/Home/Index.aspx
    ~/Views/Home/Index.ascx
    ~/Views/Shared/Index.aspx
    ~/Views/Shared/Index.ascx
    ~/Views/Home/Popup_Layout.master
    ~/Views/Shared/Popup_Layout.master
    ~/Views/Home/Popup_Layout.cshtml
    ~/Views/Home/Popup_Layout.vbhtml
    ~/Views/Shared/Popup_Layout.cshtml
    ~/Views/Shared/Popup_Layout.vbhtml]
    System.Web.Mvc.ViewResult.FindView(ControllerContext context) +399386

As an alternative, layout can be determined in View itself with assignment ~/Views/Shared/Popup_Layout.cshtml to Layout property, but it also throws an error.

I didn't find the solution using web, so I hope someone can help me here. Thanks

Upvotes: 1

Views: 330

Answers (2)

Jasen
Jasen

Reputation: 14250

Try deleting Popup_Layout.cshtml then instead of copy pasting the _Layout.cshtml create a new empty view page for Popup_Layout. After you have a new empty view page copy paste the content (if you wish). I don't know why but I tried it on my test project and it worked. I used a regular web app (not an intranet app) but I experienced the same missing view error you described.

I think the key is the way the view file was created. I copied the file and renamed it and seen the same error. When I properly created a new view and then copied in the content it all worked.

Upvotes: 1

Atul Pandey
Atul Pandey

Reputation: 11

Dear noviKorisnik i tried to replicate your error as per you written here but i am not getting the error as you specified . Your error first line is The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched: ~/Views/Home/Index.aspx

I believe mistakenly you deleted the index.cshtml . Please be sure you have index.cshtml in the right folder .if you still get the error please give more detail .

Upvotes: 0

Related Questions