Created
October 2, 2015 13:28
-
-
Save fredrikhaglund/6a9241e7334073d6ee75 to your computer and use it in GitHub Desktop.
EPiServer Partial Route Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web.Mvc; | |
using EPiServer; | |
using EPiServer.Core; | |
using EPiServer.Framework.DataAnnotations; | |
using EPiServer.Web; | |
using EPiServer.Web.Mvc; | |
using EPiServer.Web.Routing; | |
using EPiServerSite9.Business; | |
using EPiServerSite9.Models.Pages; | |
namespace EPiServerSite9.Controllers | |
{ | |
public class ExternalStuffController : Controller, IRenderTemplate<ExternalStuff> | |
{ | |
public ActionResult Index() | |
{ | |
/* Implementation of action. You can create your own view model class that you pass to the view or | |
* you can pass the page type for simpler templates */ | |
//You get the routed custom data from extension method in EPiServer.Web.Routing | |
var externalStuff2 = Request.RequestContext.GetRoutedData<ExternalStuff>(); | |
//var newsPage = Request.RequestContext.GetRoutedData<NewsPage>(); | |
return View(externalStuff2); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@using EPiServer.Web.Routing | |
@using EPiServerSite9.Business | |
@model EPiServerSite9.Business.ExternalStuff | |
@{ | |
Layout = null; | |
} | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>title</title> | |
</head> | |
<body> | |
<div> | |
<h1>Hello World</h1> | |
<p>@Model.Brand - @Model.Model</p> | |
@{ | |
var ext = new ExternalStuff | |
{ | |
Brand = "TestBrand", | |
Model = "TestModel" | |
}; | |
} | |
<a href="@UrlResolver.Current.GetVirtualPathForNonContent(ext, null, null).GetUrl()">Test Link</a> | |
</div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Routing; | |
using EPiServer.Core; | |
using EPiServer.Web.Routing; | |
using EPiServer.Web.Routing.Segments; | |
using EPiServerSite9.Models.Pages; | |
namespace EPiServerSite9.Business | |
{ | |
public class ExternalStuff | |
{ | |
public string Brand { get; set; } | |
public string Model { get; set; } | |
} | |
public class NewsPartialRouter : IPartialRouter<NewsPage, ExternalStuff> | |
{ | |
private ContentReference _newsContainer; | |
public NewsPartialRouter(ContentReference newsContainer) | |
{ | |
_newsContainer = newsContainer; | |
} | |
#region RoutePartial | |
public object RoutePartial(NewsPage content, SegmentContext segmentContext) | |
{ | |
//The format we handle is category/Name/ | |
var externalStuff = new ExternalStuff(); | |
//Use helper method GetNextValue to get the next part from the URL | |
var nextSegment = segmentContext.GetNextValue(segmentContext.RemainingPath); | |
// ... Load for other system ... | |
externalStuff.Brand = nextSegment.Next; | |
nextSegment = segmentContext.GetNextValue(nextSegment.Remaining); | |
externalStuff.Model = nextSegment.Next; | |
segmentContext.RemainingPath = nextSegment.Remaining; | |
return externalStuff; | |
} | |
#endregion | |
#region GetPartialVirtualPath | |
public PartialRouteData GetPartialVirtualPath(ExternalStuff content, string language, RouteValueDictionary routeValues, RequestContext requestContext) | |
{ | |
if (ContentReference.IsNullOrEmpty(_newsContainer)) | |
{ | |
throw new InvalidOperationException("property NewsContainer must be set on start page"); | |
} | |
return new PartialRouteData() | |
{ | |
BasePathRoot = _newsContainer, | |
PartialVirtualPath = string.Format("{0}/{1}/", | |
content.Brand, | |
content.Model) | |
}; | |
} | |
#endregion | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.Routing; | |
using EPiServer; | |
using EPiServer.Core; | |
using EPiServer.DataAccess; | |
using EPiServer.Framework; | |
using EPiServer.Web.Routing; | |
using EPiServerSite9.Models.Pages; | |
namespace EPiServerSite9.Business | |
{ | |
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))] | |
public class InitializationModule : IInitializableModule | |
{ | |
public void Initialize(EPiServer.Framework.Initialization.InitializationEngine context) | |
{ | |
ContentReference newsRootPage = new ContentReference(23); | |
var partialRouter = new NewsPartialRouter(newsRootPage); | |
RouteTable.Routes.RegisterPartialRouter<NewsPage, ExternalStuff>(partialRouter); | |
} | |
public void Preload(string[] parameters) | |
{ | |
} | |
public void Uninitialize(EPiServer.Framework.Initialization.InitializationEngine context) | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment