Interface IPartialRouter<TContent, TRoutedData>

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

Namespace: EPiServer.Web.Routing
Assembly: EPiServer.Cms.AspNet.dll
Version: 11.20.7
Syntax
public interface IPartialRouter<TContent, TRoutedData>
    where TContent : class, IContent where TRoutedData : class
Type Parameters
Name Description
TContent

The type of IContent for which routing can be extended.

TRoutedData

The type of IContent for which outgoing URL generation can be extended.

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 &apos;http://sitehost/News/Sport/A%20News/&apos;. In the example
  the URL part &apos;http://sitehost/News/&apos; is to a page of type NewsContainer (the reference to that page is given by field _newsContainer)
  while the part &apos;Sport/A%20News&apos; is handled by the partial router.
    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

}

Methods

GetPartialVirtualPath(TRoutedData, String, RouteValueDictionary, RequestContext)

Gets a partial virtual path for a content item during routing.

Declaration
PartialRouteData GetPartialVirtualPath(TRoutedData content, string language, RouteValueDictionary routeValues, RequestContext requestContext)
Parameters
Type Name Description
TRoutedData content

The content to generate a virtual path for.

System.String language

The language to generate the url for.

System.Web.Routing.RouteValueDictionary routeValues

The route values.

System.Web.Routing.RequestContext requestContext

The request context.

Returns
Type Description
PartialRouteData

A PartialRouteData containing the partial virtual path for the content and a ContentReference to the item to get base path from or null if the remaining part did not match.

Remarks

During construction of an URL for a IContent instance there is a check if any IPartialRouter instance is registered for the type of the content. If so the IPartialRouter instance is called to generate a partial path of the URL.

An example would be the generation of an url like http://sitehost/aPage/extendedPart/extendedContent/. If there is an IPartialRouter registered for the type matching extendedContent that instance will be called to generate a partial virtual path. In the example the implementation could return PartialVirtualPath as 'extendedPart/extendedContent/' and BasePathRoot set as a reference to the page that matches the url http://sitehost/aPage/.

Examples
  The below example is a partial router that generates partial URLs like &apos;Sport/A%20News/&apos;. In the example
  the URL part &apos;http://sitehost/News/&apos; is to a page of type NewsContainer the reference to that page is given by field _newsContainer.
  The part &apos;Sport/A%20News&apos; is handled by the partial router.
        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))
};
}

RoutePartial(TContent, SegmentContext)

Implement to take care of partial routing below a routed content instance.

Declaration
object RoutePartial(TContent content, SegmentContext segmentContext)
Parameters
Type Name Description
TContent content

The content that the page route has been able to route to.

SegmentContext segmentContext

The segment context containing the remaining part of url.

Returns
Type Description
System.Object

A ContentReference to the mathced data or null if the remaining part did not match.

Remarks

During routing of an incoming request first the url will be parsed from left for pages as long as there is matches. If there is a remaining part (not matched by any page) after page routing then there is a check if there is any IPartialRouter registered for the the type of the found page. If so that implementation is called to se if it can route the remaining part.

An example would be an url like http://sitehost/aPage/extendedPart/extendedContent/. If there is a page matching url http://sitehost/aPage/ then if an IPartialRouter is registered for the type matching aPage that instance will be called to route the part extendedPart/extendedContent/.

Most commonly the returned data is of type TRoutedData. But there might be cases where it is something else, an example is in the Relate package where the returned object is in parameter content.

Examples
  The below example is a partial router that routes URLs like &apos;http://sitehost/News/Sport/A%20News/&apos;. In the example
  the URL part &apos;http://sitehost/News/&apos; is to a page of type NewsContainer while the part &apos;Sport/A%20News&apos; is handled
  by the partial router.
        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;
}

Extension Methods