Hcabnettek
Hcabnettek

Reputation: 12928

Asp.Net MVC3 Razor help, the wrong _ViewStart.cshtml is being called

I have a controller 'QUnitController', then I have a folder in my Views folder 'QUnit'. In that folder I have a _ViewStart.cshtml thats coded to use the _Layout file within this folder for the Views in the QUnit folder. For some reason the _ViewStart.cshtml in the Views folder is being called and not my _ViewStart.cshtml within the QUnit folder. What am I doing wrong here? I thought having this file in the folder would override the one in the route. I can place break points in the _ViewStart files and I see the one I want is never being hit. Can anyone tell me what I'm doing wrong?

Here is controller code. QUnitController.cs

 public class QUnitController : Controller
{
    public ActionResult LoadView(string viewName)
    {
        return View(viewName);
    }
}

Here is route Views/_ViewStart.cshtml

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

}

Here is qunit Views/QUnit/_ViewStart.cshtml

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

}

Why am I not calling the correct _ViewStart? Can anyone help me correct the problem? I appreciate any help or tips anyone may have.

Thanks,
~ck in San Diego

Upvotes: 3

Views: 3167

Answers (2)

cheny
cheny

Reputation: 2725

Each area calls its own _ViewStart.cshtml. So in multi-areas context, we need to copy the _ViewStart.cshtml in the ~/View to ~/Areas/YourArea/Views.

Upvotes: 1

SLaks
SLaks

Reputation: 887315

All _ViewStart files in every parent folder are executed; the outermost one is executed first.

Your ~/Views/QUnit/_ViewStart.cshtml should be executed last.

Are you sure that your view is coming from the correct folder?

Upvotes: 4

Related Questions