Is there a way to reuse editor and display templates in asp mvc applications?

We are developing 3 asp mvc applications, they will require the same general editor and viewing templates. Instead of copying / pasting them into all 3 projects, is it possible to somehow place them in a common component and somehow refer to them in all applications?

+3
source share
3 answers

You will need to create your own ViewEngineif you want to switch to viewing from a location 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 register ViewEnginein Global.asax:

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

, . ( , , )

. , , ASP.NET (. Eilon ).
, , .

+1

( ) ASP.NET, , : http://aspadvice.com/blogs/ssmith/archive/2006/10/05/Tip_3A00_-Share-User-Controls-Between-Applications-in-ASP.NET.aspx

The main idea is to put the ASCX files in a folder and make this folder into a virtual folder of other applications (you can have several virtual folders pointing to the same physical folder).

0
source

Source: https://habr.com/ru/post/1727164/


All Articles