开发者

Is there any way to re-use editor & display templates in asp mvc applications?

开发者 https://www.devze.com 2022-12-15 20:38 出处:网络
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

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?


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.


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:

  • Create a class library project
  • Create your templates as normal and mark them as 'Embedded resource'. This will make sure they will be put in the library's DLL
  • Write your custom view engine to pull the templates from the assembly (DLL). A good start might be editing/forking an existing one: http://code.google.com/p/string-template-view-engine-mvc/source/browse/trunk/StringTemplateViewEngine/StringTemplateViewEngine.cs
  • Then register your view engine in your ASP.NET MVC project


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消