views:

158

answers:

4

We're developing 3 asp mvc applications, all will require some of the same shared editor & display template views. Rather than copy/paste these across all 3 projects, is it possible somehow to put them in a shared component and reference them somehow in all applications?

A: 

You're going to need to create your own ViewEngine if you want to take to views from a place other than the Views folders.

public class CustomViewEngine : WebFormViewEngine {
    public CustomViewEngine() : base() {

        MasterLocationFormats = new[] {
            "/YourFolder/{1}/{0}.master",
            "/YourFolder/Shared/{0}.master"
        };

        ViewLocationFormats = new[] {
            "/YourFolder/{1}/{0}.aspx",
            "/YourFolder/{1}/{0}.ascx",
            "/YourFolder/Shared/{0}.aspx",
            "/YourFolder/Shared/{0}.ascx"
        };

        PartialViewLocationFormats = ViewLocationFormats;
    }

    //Override the FindView method to implement your own logic
    public override ViewEngineResult FindView(
        ControllerContext controllerContext, string viewName, 
        string masterName, bool useCache)
        return base.FindView(controllerContext, viewName, masterName, useCache);
    }
}

Then to register your ViewEngine in the Global.asax :

protected void Application_Start() {
    ViewEngines.Engines.Clear();
    ViewEngines.Engines.Add(new CustomViewEngine());
}

Also, this post might be helpful. (too bad the download link at the bottom is broken though)

Edit : It appears this solution won't work in practice, since ASP.NET won't allow loading content from outside of the application's directory (see Eilon's comments).
But the link above, storing views in DB, could still be helpful.

çağdaş
This doesn't solve the problem at hand. It still won't load user controls from other applications (or from any folder outside the application).
Eilon
@Eilon, don't you mean the shared views by user controls?
çağdaş
I mean that your solution just allows you to put the user controls in other files within the *same* application. The original question asked for the ability for *multiple* applications to share the same user controls.
Eilon
I see. Well, I didn't actually test this. But the idea was that the path of a shared folder between the applications would be set via `MasterLocationFormats` and `ViewLocationFormats` properties. And those shared controls would be in that folder.
çağdaş
It's a good idea in theory, but unfortunately ASP.NET will refuse to load content from outside of the application's directory (for security reasons). That's why you need to map a virtual path into both folders: It ensures that all the paths map as children to the app's main folder.
Eilon
I understand. I'll edit the answer to note that.
çağdaş
A: 

To share user controls (or almost any content) between multiple ASP.NET applications you can use the trick listed here: http://aspadvice.com/blogs/ssmith/archive/2006/10/05/Tip_3A00_-Share-User-Controls-Between-Applications-in-ASP.NET.aspx

The basic idea is to place the ASCX files in a folder and make that folder in to a virtual folder of other applications (you can have multiple virtual folders pointing at the same physical folder).

Eilon
+1  A: 

I am not sure how it can be done but there must be a way to compile the pages in a DLL and share the library.

But if you can afford to use custom view engine and another template system (for example Stringtemplate) then you can share the views as if you are sharing a library:

Maxwell Troy Milton King
A: 

More fiddling to make stuff work...

TheBean