有什么办法可以重用编辑器和编辑器吗?在 asp mvc 应用程序中显示模板?

发布于 2024-08-16 11:17:43 字数 126 浏览 1 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

惯饮孤独 2024-08-23 11:17:43

如果您想从 Views 文件夹以外的位置获取视图,则需要创建自己的 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);
    }
}

然后在 Global.asax 中注册您的 ViewEngine

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

另外,这篇文章可能会有所帮助。 (可惜底部的下载链接已损坏)

编辑:看来此解决方案在实践中不起作用,因为 ASP.NET 不允许从应用程序目录外部加载内容(请参阅 Eilon 的评论)。
但上面的链接,将视图存储在数据库中,仍然可能有帮助。

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.

橘香 2024-08-23 11:17:43

我不知道如何做到这一点,但必须有一种方法来编译 DLL 中的页面并共享库。

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

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:

甩你一脸翔 2024-08-23 11:17:43

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

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文