Optimized HomeAlias exported XML a bit.

Also fixed an issue when a custom route contains an empty value for a route value entry (e.g. id=null).
This commit is contained in:
Sipke Schoorstra
2015-09-07 17:57:36 +01:00
parent d81e32d3b8
commit a760f012f2
3 changed files with 23 additions and 12 deletions

View File

@@ -31,7 +31,7 @@ namespace Orchard.Autoroute.Recipes.Builders {
public override void Build(BuildContext context) { public override void Build(BuildContext context) {
var homeAliasRoute = _homeAliasService.GetHomeRoute() ?? new RouteValueDictionary(); var homeAliasRoute = _homeAliasService.GetHomeRoute() ?? new RouteValueDictionary();
var root = new XElement("HomeAlias", homeAliasRoute.Select(x => new XElement(Capitalize(x.Key), x.Value))); var root = new XElement("HomeAlias");
var homePage = _homeAliasService.GetHomePage(VersionOptions.Latest); var homePage = _homeAliasService.GetHomePage(VersionOptions.Latest);
// If the home alias points to a content item, store its identifier in addition to the routevalues, // If the home alias points to a content item, store its identifier in addition to the routevalues,
@@ -41,6 +41,10 @@ namespace Orchard.Autoroute.Recipes.Builders {
var homePageIdentifier = _contentManager.GetItemMetadata(homePage).Identity.ToString(); var homePageIdentifier = _contentManager.GetItemMetadata(homePage).Identity.ToString();
root.Attr("Id", homePageIdentifier); root.Attr("Id", homePageIdentifier);
} }
else {
// The alias does not point to a content item, so export the route values instead.
root.Add(homeAliasRoute.Select(x => new XElement(Capitalize(x.Key), x.Value)).ToArray());
}
context.RecipeDocument.Element("Orchard").Add(root); context.RecipeDocument.Element("Orchard").Add(root);
} }

View File

@@ -20,15 +20,16 @@ namespace Orchard.Autoroute.Recipes.Executors {
public override void Execute(RecipeExecutionContext context) { public override void Execute(RecipeExecutionContext context) {
var root = context.RecipeStep.Step; var root = context.RecipeStep.Step;
var routeValueDictionary = root.Elements().ToDictionary(x => x.Name.LocalName.ToLower(), x => (object)x.Value);
var homePageIdentifier = root.Attr("Id"); var homePageIdentifier = root.Attr("Id");
var homePageIdentity = new ContentIdentity(homePageIdentifier); var homePageIdentity = !String.IsNullOrWhiteSpace(homePageIdentifier) ? new ContentIdentity(homePageIdentifier) : default(ContentIdentity);
var homePage = !String.IsNullOrEmpty(homePageIdentifier) ? _contentManager.ResolveIdentity(homePageIdentity) : default(ContentItem); var homePage = homePageIdentity != null ? _contentManager.ResolveIdentity(homePageIdentity) : default(ContentItem);
if (homePage != null) if (homePage != null)
_homeAliasService.PublishHomeAlias(homePage); _homeAliasService.PublishHomeAlias(homePage);
else else {
var routeValueDictionary = root.Elements().ToDictionary(x => x.Name.LocalName.ToLower(), x => (object) x.Value);
_homeAliasService.PublishHomeAlias(new RouteValueDictionary(routeValueDictionary)); _homeAliasService.PublishHomeAlias(new RouteValueDictionary(routeValueDictionary));
}
} }
} }
} }

View File

@@ -1,4 +1,5 @@
using System.Web.Routing; using System;
using System.Web.Routing;
using Orchard.Alias; using Orchard.Alias;
using Orchard.ContentManagement; using Orchard.ContentManagement;
@@ -20,12 +21,8 @@ namespace Orchard.Autoroute.Services {
public int? GetHomePageId() { public int? GetHomePageId() {
var homePageRoute = GetHomeRoute(); var homePageRoute = GetHomeRoute();
var homePageId = var homePageIdValue = homePageRoute != null && homePageRoute.ContainsKey("id") ? (string)homePageRoute["id"] : default(string);
homePageRoute != null var homePageId = TryParseInt32(homePageIdValue);
? homePageRoute.ContainsKey("id")
? XmlHelper.Parse<int>((string)homePageRoute["id"])
: default(int?)
: default(int?);
return homePageId; return homePageId;
} }
@@ -51,5 +48,14 @@ namespace Orchard.Autoroute.Services {
_aliasService.DeleteBySource(AliasSource); _aliasService.DeleteBySource(AliasSource);
_aliasService.Set(HomeAlias, route, AliasSource); _aliasService.Set(HomeAlias, route, AliasSource);
} }
private int? TryParseInt32(string value) {
int i;
if (String.IsNullOrWhiteSpace(value) || !Int32.TryParse(value, out i))
return null;
return i;
}
} }
} }