Embedded resources and Layout pages

Feb 27, 2014 at 8:47 AM
I have created a MVC site that uses MvcCodeRouting to register routes for 2 'modules' - which are MVC sites where the content items are all set to 'Embedded Resource'.

A simple view works great, but I have a problem when I give that view a layout. The layout is also an embedded resource.

Currently my view looks like:

@model dynamic

ViewBag.Title = "A Module";
Layout = "~/Views/_LayoutPage.cshtml";

But at runtime I get:

The layout page "~/Views/_LayoutPage.cshtml" could not be found at the following path: "~/Views/_LayoutPage.cshtml".

I expect I need a different syntax/path for the layout page, but I don't know where to start with working this out. Is there a document describing how paths should be specified?

Note, the Layout page is in my assembly at 'Workstations.Views._LayoutPage.cshtml'
Feb 27, 2014 at 3:09 PM
There's no special syntax/path needed. "~/Views/_LayoutPage.cshtml" is an absolute path, if the file is in the Workstations assembly, and that assembly was registered using a base route (e.g. "Workstations"), then the path should be "~/Views/Workstations/_LayoutPage.cshtml". I do not recommend you do this, because you are introducing a dependency between the Workstations module and the host application, where you are hard-coding the base route, which is something the host application should freely decide what to use. Instead I recommend you use a relative path for the layout. You can also embed a _ViewStart.cshtml page to avoid having to set the layout on every page.

Let me know if this helps.
Feb 27, 2014 at 5:19 PM
You got me thinking, maybe the tilde should be baseRoute-relative for views location, like it is for URL generation and custom routes. It would be a breaking change though, so it'll have to wait for v2. I'll think about it.
Feb 28, 2014 at 6:39 AM
Thanks for your help - what does the tilde do currently?
I have seen ~ and ~~ in some posts - I haven't got my head around what the effect of using this is!

A relative path works fine, as you suggested.

This is fine for my immediate requirements. Thanks
Marked as answer by maxtoroq on 2/28/2014 at 6:20 AM
Feb 28, 2014 at 1:21 PM
In URL generation, ~ goes to the root of the module and ~~ goes to the root of the application.
Feb 28, 2014 at 1:22 PM
Perfect - that makes sense.