TruMan1
TruMan1

Reputation: 36128

Use MasterPage from separate library for MasterPageFile?

Is there a way to specify a MasterPage from a separate library to use as the base template? I tried below but it says it can't find the path (which is obvious because it's a namespace):

<%@ Master Language="C#" MasterPageFile="MyClassLib.Templates.Base.master" %>

I'm trying to nest MasterPages, but for the base MasterPage, I would like to reside it in a separate class library for easy distribution. Then on target web apps, I would like them to be able to descend from my base MasterPage which is in a DLL. Anyone know how to do this?

Upvotes: 1

Views: 540

Answers (1)

Ant P
Ant P

Reputation: 25231

I tried below but it says it can't find the path (which is true because it's a namespace)

That's your problem. MasterPagePath is supposed to be the virtual path to the physical .master file - you can't mash namespaces and file paths together and expect them to work.

There are a few ways you can do what you're doing. The first is simply to distribute the same master page file across all of the applications that need it. This might scream duplication at you but, in actual fact, it is probably cleaner and more manageable than the alternatives. There should only really ever be a couple of duplicates, otherwise I'd question why you need the same master page in so many different places (i.e. perhaps your solution architecture is the underlying problem).

Alternatively, you can use virtual paths to reference a master page from a different web project and designate one as the parent.

To achieve what you really want, however, you will need to embed the master page in the DLL and then create a VirtualPathProvider to locate the embedded resource as if it were a physical resource at a virtual path. There's a tutorial for this here. However, I would heed the drawbacks mentioned (the lack of support for designer views and inability to set the master page in the Page directive).

Generally, I'd say you shouldn't really need to do this and I'd look - if at all possible - at addressing your solution architecture so you don't need to.

Upvotes: 1

Related Questions