Skip to content

Commit a9d8f5f

Browse files
Fix exporting/importing macro partial views using a virtual file path
1 parent 8b853b1 commit a9d8f5f

File tree

2 files changed

+15
-30
lines changed

2 files changed

+15
-30
lines changed

src/Umbraco.Core/Packaging/PackagesRepository.cs

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,10 @@ private void PackageMacros(PackageDefinition definition, XContainer root)
414414

415415
root.Add(macros);
416416

417-
// get the partial views for macros and package those
418-
IEnumerable<string> views = packagedMacros.Select(x => x.MacroSource).Where(x => x.EndsWith(".cshtml"));
419-
PackageMacroPartialViews(views, root);
417+
// Get the partial views for macros and package those (exclude views outside of the default directory, e.g. App_Plugins\*\Views)
418+
IEnumerable<string> views = packagedMacros.Where(x => x.MacroSource.StartsWith(Constants.SystemDirectories.MacroPartials))
419+
.Select(x => x.MacroSource.Substring(Constants.SystemDirectories.MacroPartials.Length).Replace('/', '\\'));
420+
PackageStaticFiles(views, root, "MacroPartialViews", "View", _fileSystems.MacroPartialsFileSystem);
420421
}
421422

422423
private void PackageStylesheets(PackageDefinition definition, XContainer root)
@@ -487,33 +488,6 @@ private void PackageTemplates(PackageDefinition definition, XContainer root)
487488
root.Add(templatesXml);
488489
}
489490

490-
private void PackageMacroPartialViews(IEnumerable<string> viewPaths, XContainer root)
491-
{
492-
var viewsXml = new XElement("MacroPartialViews");
493-
foreach (var viewPath in viewPaths)
494-
{
495-
// TODO: See TODO note in MacrosController about the inconsistencies of usages of partial views
496-
// and how paths are saved. We have no choice currently but to assume that all views are 100% always
497-
// on the content path.
498-
499-
var physicalPath = _hostingEnvironment.MapPathContentRoot(viewPath);
500-
if (!File.Exists(physicalPath))
501-
{
502-
throw new InvalidOperationException("Could not find partial view at path " + viewPath);
503-
}
504-
505-
var fileContents = File.ReadAllText(physicalPath, Encoding.UTF8);
506-
507-
viewsXml.Add(
508-
new XElement(
509-
"View",
510-
new XAttribute("path", viewPath),
511-
new XCData(fileContents)));
512-
}
513-
514-
root.Add(viewsXml);
515-
}
516-
517491
private void PackageDocumentTypes(PackageDefinition definition, XContainer root)
518492
{
519493
var contentTypes = new HashSet<IContentType>();

src/Umbraco.Infrastructure/Packaging/PackageDataInstallation.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,6 +1270,17 @@ public IReadOnlyList<IPartialView> ImportMacroPartialViews(IEnumerable<XElement>
12701270
throw new InvalidOperationException("No path attribute found");
12711271
}
12721272

1273+
// Remove prefix to maintain backwards compatibility
1274+
if (path.StartsWith(Constants.SystemDirectories.MacroPartials))
1275+
{
1276+
path = path.Substring(Constants.SystemDirectories.MacroPartials.Length);
1277+
}
1278+
else if (path.StartsWith("~"))
1279+
{
1280+
_logger.LogWarning("Importing macro partial views outside of the Views/MacroPartials directory is not supported: {Path}", path);
1281+
continue;
1282+
}
1283+
12731284
IPartialView macroPartialView = _fileService.GetPartialViewMacro(path);
12741285

12751286
// only update if it doesn't exist

0 commit comments

Comments
 (0)