IPartialRouter(TContent, TRoutedData) Interface Interface

Assembly: EPiServer (in EPiServer.dll) Version: 10.10.4.0 (10.10.4.0)

Syntax:

C#
public interface IPartialRouter<TContent, TRoutedData>
where TContent : class, IContent
where TRoutedData : class

Defines the signature for a component that extends routing to IContent.

Methods

Name
GetPartialVirtualPath
RoutePartial

Remarks

TContent specifies for which content types this implementation can partially route. An example would be an url like 'http://sitehost/aPage/extendedPart/extendedContent/'. If there is a page matching url 'http://sitehost/aPage/' then if the page can be assigned to TContent the instance of IPartialRouter can be called to route the part 'extendedPart/extendedContent/'.

TRoutedData specifies for which content types this implementation can extend outgoing url for. An example would be the generation of an url like 'http://sitehost/aPage/extendedPart/extendedContent/'. If TRoutedData is assignable for the type matching extendedContent then this instance can be called to generate a partial virtual path.

Examples

The below example is a partial router that routes URLs like 'http://sitehost/News/Sport/A%20News/'. In the example the URL part 'http://sitehost/News/' is to a page of type NewsContainer (the reference to that page is given by field _newsContainer) while the part 'Sport/A%20News' is handled by the partial router.

CopyC#
public class NewsPartialRouter : IPartialRouter<NewsContainer, NewsContent>
{
    private NewsContentStore _newsStore;
    private ContentReference _newsContainer;

    public NewsPartialRouter(NewsContentStore newsStore, ContentReference newsContainer)
    {
        _newsStore = newsStore;
        _newsContainer = newsContainer;
    }

    #region RoutePartial
    public object RoutePartial(NewsContainer content, SegmentContext segmentContext)
    {
        //The format we handle is category/Name/
        NewsContent newsContent = null;

        //Use helper method GetNextValue to get the next part from the URL
        var nextSegment = segmentContext.GetNextValue(segmentContext.RemainingPath);

        NewsCateory category;
        if (Enum.TryParse<NewsCateory>(nextSegment.Next, out category))
        {
            nextSegment = segmentContext.GetNextValue(nextSegment.Remaining);
            if (!String.IsNullOrEmpty(nextSegment.Next))
            {
                newsContent = _newsStore.RouteContent(category, HttpUtility.UrlDecode(nextSegment.Next));
                if (newsContent != null)
                {
                    //Update RemainingPath so the part that we have handled is removed.
                    segmentContext.RemainingPath = nextSegment.Remaining;
                }
            }
        }

        return newsContent;
    }
    #endregion

    #region GetPartialVirtualPath
    public PartialRouteData GetPartialVirtualPath(NewsContent 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.Category.ToString(),
                HttpUtility.UrlPathEncode(content.Name))
        };
    }
    #endregion

}