Existing Views Are Overwritten When Creating a New Template
See original GitHub issueLinked to #3494
This issue was closed at the time and haven’t been able to re-open so just raising the issue again here (and have updated for Umbraco 8).
Bug summary
When creating a new template via code, if a view exists with the same name, it is overwritten. I believe there should be a check in place that if the view exists, it’s left alone but the template is added to the database regardless. If the file doesn’t exist, only then should it create a new, blank one.
Specifics
Umbraco version: 8.1.2
Steps to reproduce
Create a new view in ~\Views called Test.cshtml (not through the CMS). Enter some content, for example:
@{
Layout = null;
}
<h2>Homepage</h2>
Create a component that creates a new template:
using Umbraco.Core;
using Umbraco.Core.Composing;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
namespace UmbracoTemplateTest.Components
{
[RuntimeLevel(MinLevel = RuntimeLevel.Run)]
public class ContentTypeComposer : IUserComposer
{
public void Compose(Composition composition)
{
composition.Components().Append<ContentTypeComponent>();
}
}
public class ContentTypeComponent : IComponent
{
private readonly IFileService _fileService;
public ContentTypeComponent(IFileService fileService)
{
_fileService = fileService;
}
public void Initialize()
{
ITemplate template = _fileService.GetTemplate("test");
if (template == null)
{
template = new Template("Test", "test");
_fileService.SaveTemplate(template);
}
}
public void Terminate() { }
}
}
See that Test.cshtml has been overwritten by a basic Umbraco template view:
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
Layout = null;
}
Expected result
When the template is saved, existing views with the same name aren’t overwritten.
Actual result
A blank/basic view is written into the contents of the view in the place of already existing content.
This was fixed when creating the template through the CMS but, if you create the file then create a template using code, the file is overwritten.
Thanks,
Ben
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (5 by maintainers)
I am not sure if the FileService should be responsible for that, but I can ask around.
For convenience it would be nice, but for now you could load the file contents yourself;
This has been fixed at various levels a few times but I guess for just specific circumstance, for example, in the implementation of
IFileService
, in the methods:CreateTemplateForContentType
,CreateTemplateWithIdentity
any existing view will not be overwritten with checks toGetViewContent
.You can utilize the
CreateTemplateWithIdentity
instead of doingand that should solve your problem.