Embedded resources and Layout pages

Imported from the CodePlex archive for reference purposes. Support for MvcCodeRouting has ended.

Commented on
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'
Commented on link
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.
Commented on link
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.
Commented on link
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
Commented on link
In URL generation, ~ goes to the root of the module and ~~ goes to the root of the application.
Commented on link
Perfect - that makes sense.