有没有办法重新使用编辑器&在asp mvc应用程序中显示模板?

时间:2010-01-03 21:31:30

标签: asp.net-mvc asp.net-mvc-2 asp.net-mvc-templates

我们正在开发3个asp mvc应用程序,所有这些都需要一些相同的共享编辑器和显示模板视图。而不是在所有3个项目中复制/粘贴这些项目,是否可能以某种方式将它们放在共享组件中并以某种方式在所有应用程序中引用它们?

3 个答案:

答案 0 :(得分:1)

如果您想从视图文件夹以外的地方拍摄视图,则需要创建自己的ViewEngine

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);
    }
}

然后在ViewEngine注册Global.asax

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

此外,this post可能会有所帮助。 (但是底部的下载链接太糟糕了)

编辑:此解决方案似乎无法在实践中使用,因为ASP.NET不允许从应用程序目录外部加载内容(请参阅Eilon的评论) 。
但上面的链接,在DB中存储视图,仍然可能会有所帮助。

答案 1 :(得分:1)

我不确定如何做到但是必须有一种方法来编译DLL中的页面并共享库。

但是如果你能负担得起使用自定义视图引擎和另一个模板系统(例如Stringtemplate),那么就像共享库一样可以共享视图:

答案 2 :(得分:0)

要在多个ASP.NET应用程序之间共享用户控件(或几乎任何内容),您可以使用此处列出的技巧:http://aspadvice.com/blogs/ssmith/archive/2006/10/05/Tip_3A00_-Share-User-Controls-Between-Applications-in-ASP.NET.aspx

基本思想是将ASCX文件放在一个文件夹中,并将该文件夹放入其他应用程序的虚拟文件夹中(您可以将多个虚拟文件夹指向同一个物理文件夹)。