Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Commit

Permalink
Added dynamic route handling
Browse files Browse the repository at this point in the history
Added tags property to blog posts
Added dynamic url handling with UmbracoVirtualNodeByIdRouteHandler to show all tags and all content related to a tag
  • Loading branch information
Mantus667 committed Aug 1, 2017
1 parent b703f0c commit 49603a5
Show file tree
Hide file tree
Showing 13 changed files with 215 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/UmbracoUrlHandling/App_Data/Models/all.dll.path
Original file line number Diff line number Diff line change
@@ -1 +1 @@
C:\Users\mantu\AppData\Local\Temp\Temporary ASP.NET Files\vs\027ab051\822a0174\App_Web_all.generated.cs.8f9494c4.oibqrept.dll
C:\Users\brendeld\AppData\Local\Temp\Temporary ASP.NET Files\vs\db0e3ea5\41b4334d\App_Web_all.generated.cs.8f9494c4.feq1txiv.dll
11 changes: 10 additions & 1 deletion src/UmbracoUrlHandling/App_Data/Models/all.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using Umbraco.ModelsBuilder;
using Umbraco.ModelsBuilder.Umbraco;
[assembly: PureLiveAssembly]
[assembly:ModelsBuilderAssembly(PureLive = true, SourceHash = "e4d2aa0bdb7a3315")]
[assembly:ModelsBuilderAssembly(PureLive = true, SourceHash = "4ad86b1aa0722510")]
[assembly:System.Reflection.AssemblyVersion("0.0.0.1")]


Expand Down Expand Up @@ -83,6 +83,15 @@ public string Introduction
get { return this.GetPropertyValue<string>("introduction"); }
}

///<summary>
/// Tags: Tags related to this blog post
///</summary>
[ImplementPropertyType("tags")]
public IEnumerable<string> Tags
{
get { return this.GetPropertyValue<IEnumerable<string>>("tags"); }
}

///<summary>
/// Url Name: The url name which should be used
///</summary>
Expand Down
11 changes: 10 additions & 1 deletion src/UmbracoUrlHandling/App_Data/Models/models.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
using Umbraco.ModelsBuilder.Umbraco;

[assembly: PureLiveAssembly]
[assembly:ModelsBuilderAssembly(PureLive = true, SourceHash = "e4d2aa0bdb7a3315")]
[assembly:ModelsBuilderAssembly(PureLive = true, SourceHash = "4ad86b1aa0722510")]
[assembly:System.Reflection.AssemblyVersion("0.0.0.2")]

namespace Umbraco.Web.PublishedContentModels
Expand Down Expand Up @@ -67,6 +67,15 @@ public string Introduction
get { return this.GetPropertyValue<string>("introduction"); }
}

///<summary>
/// Tags: Tags related to this blog post
///</summary>
[ImplementPropertyType("tags")]
public IEnumerable<string> Tags
{
get { return this.GetPropertyValue<IEnumerable<string>>("tags"); }
}

///<summary>
/// Url Name: The url name which should be used
///</summary>
Expand Down
2 changes: 1 addition & 1 deletion src/UmbracoUrlHandling/App_Data/Models/models.hash
Original file line number Diff line number Diff line change
@@ -1 +1 @@
e4d2aa0bdb7a3315
4ad86b1aa0722510
Binary file modified src/UmbracoUrlHandling/App_Data/Umbraco.sdf
Binary file not shown.
42 changes: 42 additions & 0 deletions src/UmbracoUrlHandling/Controller/BlogPostRepositoryController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Linq;
using System.Web.Mvc;
using Umbraco.Web.Models;
using Umbraco.Web.Mvc;
using UmbracoUrlHandling.Models;

namespace UmbracoUrlHandling.Controller
{
/// <summary>
/// Route hijacking using controller to handle custom model building for templates
/// </summary>
/// <seealso cref="Umbraco.Web.Mvc.RenderMvcController" />
public class BlogPostRepositoryController : RenderMvcController
{
/// <summary>
/// Method to show tags overview template.
/// </summary>
/// <param name="model">The model.</param>
/// <returns></returns>
public ActionResult Categories(RenderModel model)
{
var tags = Services.TagService.GetAllContentTags().ToList();
var viewModel = new TagsOverviewViewModel(model.Content) {Tags = tags};
return View("TagsOverview", viewModel);
}

/// <summary>
/// Shows all content that was tagged with a specific category.
/// </summary>
/// <param name="model">The model.</param>
/// <param name="category">The category.</param>
/// <returns></returns>
public ActionResult Category(RenderModel model, string category)
{
var tagsService = Services.TagService;
var relatedContentTaggedEntities = tagsService.GetTaggedContentByTag(category);
var relatedContent = Umbraco.TypedContent(relatedContentTaggedEntities.Select(x => x.EntityId));
var viewModel = new CategoryContentViewModel(model.Content) { Category = category, RelatedContentForCategory = relatedContent};
return View("RelatedContentForCategory", viewModel);
}
}
}
37 changes: 37 additions & 0 deletions src/UmbracoUrlHandling/Models/CategoryContentViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.Collections.Generic;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;

namespace UmbracoUrlHandling.Models
{
/// <summary>
/// ViewModel to render all content for aspecific category
/// </summary>
/// <seealso cref="Umbraco.Core.Models.PublishedContent.PublishedContentWrapped" />
public class CategoryContentViewModel : PublishedContentWrapped
{
/// <summary>
/// Initializes a new instance of the <see cref="CategoryContentViewModel"/> class.
/// </summary>
/// <param name="content">The content to wrap and extend.</param>
public CategoryContentViewModel(IPublishedContent content) : base(content)
{
}

/// <summary>
/// Gets or sets the category.
/// </summary>
/// <value>
/// The category.
/// </value>
public string Category { get; set; }

/// <summary>
/// Gets or sets the related content for category.
/// </summary>
/// <value>
/// The related content for category.
/// </value>
public IEnumerable<IPublishedContent> RelatedContentForCategory { get; set; }
}
}
29 changes: 29 additions & 0 deletions src/UmbracoUrlHandling/Models/TagsOverviewViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Collections.Generic;
using Umbraco.Core.Models;
using Umbraco.Core.Models.PublishedContent;

namespace UmbracoUrlHandling.Models
{
/// <summary>
/// View model for tags overview of blog posts
/// </summary>
/// <seealso cref="Umbraco.Core.Models.PublishedContent.PublishedContentWrapped" />
public class TagsOverviewViewModel : PublishedContentWrapped
{
/// <summary>
/// Initializes a new instance of the <see cref="TagsOverviewViewModel"/> class.
/// </summary>
/// <param name="content">The content to wrap and extend.</param>
public TagsOverviewViewModel(IPublishedContent content) : base(content)
{
}

/// <summary>
/// Gets or sets the tags.
/// </summary>
/// <value>
/// The tags.
/// </value>
public IEnumerable<ITag> Tags { get; set; }
}
}
19 changes: 19 additions & 0 deletions src/UmbracoUrlHandling/RouteHandler/BlogRepositoryRouteHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Umbraco.Web.Mvc;

namespace UmbracoUrlHandling.RouteHandler
{
/// <summary>
/// BlogPostRepositoryrouteHandler to handle routes to BlogPostRepository
/// </summary>
/// <seealso cref="Umbraco.Web.Mvc.UmbracoVirtualNodeByIdRouteHandler" />
public class BlogRepositoryRouteHandler : UmbracoVirtualNodeByIdRouteHandler
{
/// <summary>
/// Initializes a new instance of the <see cref="BlogRepositoryRouteHandler"/> class.
/// </summary>
/// <param name="realNodeId">The real node identifier.</param>
public BlogRepositoryRouteHandler(int realNodeId) : base(realNodeId)
{
}
}
}
34 changes: 33 additions & 1 deletion src/UmbracoUrlHandling/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Web;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Umbraco.Core;
Expand Down Expand Up @@ -27,6 +28,7 @@ public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, App

ContentFinderResolver.Current.InsertTypeBefore<ContentFinderByNotFoundHandlers, BlogPostContentFinder>();

//Make a route based on a node Id
RouteTable.Routes.MapUmbracoRoute(
"ProductRoute",
"Products/{sku}",
Expand All @@ -43,6 +45,36 @@ public void OnApplicationStarting(UmbracoApplicationBase umbracoApplication, App
public void OnApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
ContentService.Published += (sender, args) => HttpContext.Current.Cache.Remove("CachedBlogPostNodes");

//Dynamic routes based on node type and node urls
var blogRepositoryNodes = UmbracoContext.Current.ContentCache.GetByXPath("//BlogPostRepository").ToArray();

foreach (var repository in blogRepositoryNodes)
{
var uri = repository.Url().TrimStart("/");
var hash = uri.GetHashCode();

RouteTable.Routes.MapUmbracoRoute(
$"blog_repository_categories_{hash}",
$"{uri.EnsureEndsWith('/')}categories/",
new
{
controller = "BlogPostRepository",
action = "Categories"
},
new BlogRepositoryRouteHandler(repository.Id));

RouteTable.Routes.MapUmbracoRoute(
$"blog_repository_category_{hash}",
$"{uri.EnsureEndsWith('/')}category/{{category}}",
new
{
controller = "BlogPostRepository",
action = "Category",
category = UrlParameter.Optional
},
new BlogRepositoryRouteHandler(repository.Id));
}
}
}
}
6 changes: 6 additions & 0 deletions src/UmbracoUrlHandling/UmbracoUrlHandling.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@
<Content Include="Views\ProductDetails.cshtml" />
<Content Include="Views\RealEstate.cshtml" />
<Content Include="Views\TextPage.cshtml" />
<Content Include="Views\TagsOverview.cshtml" />
<Content Include="Views\RelatedContentForCategory.cshtml" />
<None Include="Web.Debug.config">
<DependentUpon>Web.config</DependentUpon>
</None>
Expand Down Expand Up @@ -331,7 +333,11 @@
<ItemGroup>
<Compile Include="ContentFinder\BlogPostContentFinder.cs" />
<Compile Include="ContentFinder\ContentFinderItem.cs" />
<Compile Include="Controller\BlogPostRepositoryController.cs" />
<Compile Include="Controller\ProductDetailsController.cs" />
<Compile Include="Models\CategoryContentViewModel.cs" />
<Compile Include="Models\TagsOverviewViewModel.cs" />
<Compile Include="RouteHandler\BlogRepositoryRouteHandler.cs" />
<Compile Include="RouteHandler\ProductRouteHandler.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SegmentProvider\ImmoUrlSegmentProvider.cs" />
Expand Down
14 changes: 14 additions & 0 deletions src/UmbracoUrlHandling/Views/RelatedContentForCategory.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@inherits UmbracoViewPage<UmbracoUrlHandling.Models.CategoryContentViewModel>
@{
Layout = "Master.cshtml";
}

<h1>Pages related to category "@Model.Category":</h1>
<ul>
@foreach (var content in Model.RelatedContentForCategory)
{
<li>
<a href="@content.Url">@content.Name</a>
</li>
}
</ul>
13 changes: 13 additions & 0 deletions src/UmbracoUrlHandling/Views/TagsOverview.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@inherits UmbracoViewPage<UmbracoUrlHandling.Models.TagsOverviewViewModel>
@{
Layout = "Master.cshtml";
}

<h1>All tags</h1>

<ul>
@foreach (var tag in Model.Tags)
{
<li><a href="@Model.Url.EnsureEndsWith("/")category/@tag.Text">@tag.Text</a></li>
}
</ul>

0 comments on commit 49603a5

Please sign in to comment.