Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Cary 21 posts 107 karma points
    Oct 10, 2020 @ 02:56
    Cary
    0

    How could I save template file (cshtml) to database instead of file.

    Hi all,

    I have published the Umbraco on Azure app services, maybe we need more than one services for that, so I want save the template file to database or blob that I can make it in sync. how should I do?

    regards cary

  • Dhanesh Kumar MJ 158 posts 511 karma points c-trib
    Oct 10, 2020 @ 04:28
    Dhanesh Kumar MJ
    0

    Hi Cary,

    You can use File service ,here https://our.umbraco.com/documentation/reference/management/services/FileService/

    Regards DK 🙂

  • Cary 21 posts 107 karma points
    Oct 10, 2020 @ 07:06
    Cary
    0

    Hi DK,

    Thanks for your help. I have read the documentation, but I still don’t know how to implement it. I haven’t seen a more complete description for the FILE service, where should I define it, and where should I use it?

    thanks

    cary

  • Dhanesh Kumar MJ 158 posts 511 karma points c-trib
    Oct 13, 2020 @ 05:29
    Dhanesh Kumar MJ
    101

    Hi Cary,

    you can achieve this by two way's.

    1. Sync all the templates to DB on an API call.
    2. Register an event in IComponent , this event will trigger once template saved from the backend.

    Please find the below samples

    1. Sync all the templates to DB on an API call.

       [RoutePrefix("syncapi")]
      public class SyncApiController:UmbracoApiController
      {
      private IFileService _fileService;
       public SyncApiController(IFileService fileService){
      
      
        _fileService = fileService;
       }
      [HttpGet]
          [Route("template")]
          public  HttpStatusCode Template()
          {
               var all templates=_fileService.GetTemplates();//get all templates
              var homeTemplate = _fileService.GetTemplate("home");  //get template with -- alias "home"
              var content=homeTemplate.content; // get content/code in home template
              return HttpStatusCode.OK;
          }}
      

    Api Call - http://localhost:1234/syncapi/template

    1. Register an event in IComponent

    Create a class file by inheriting IComponent & Register

    Subscribing an Event

        // initialize: runs once when Umbraco starts
        public void Initialize()
        {
            RouteTable.Routes.MapMvcAttributeRoutes();
            log4net.Config.XmlConfigurator.Configure();
            GlobalConfiguration.Configuration.MapHttpAttributeRoutes();
            GlobalConfiguration.Configuration.Initializer(GlobalConfiguration.Configuration);
            GlobalConfiguration.Configuration.EnsureInitialized();
            //GlobalConfiguration.Configure(Register);
            // GlobalConfiguration.Configuration.EnableCors();
    
            FileService.SavedTemplate += Template_Saved;
    
        }
    
        private void Template_Saved(IFileService sender, SaveEventArgs<ITemplate> e)//this will trigger once the template saved
        {
            foreach (var savedItem in e.SavedEntities)
            {
                var content = savedItem.Content;
                var path = savedItem.Path;
            }
        }
        public static void Register(HttpConfiguration config)
        {
            var corsAttr = new System.Web.Http.Cors.EnableCorsAttribute("*", "*", "*");
            config.EnableCors(corsAttr);
        }
        public void Terminate()
        {
            FileService.SavedTemplate -= Template_Saved;
        }
    }
    

    Regards DK :)

  • Cary 21 posts 107 karma points
    Oct 13, 2020 @ 06:47
    Cary
    0

    Hi DK,

    Thanks for your excellent answer, I will take a try, thanks again.

    Best Regards

    Cary

Please Sign in or register to post replies

Write your reply to:

Draft