Quantcast
Channel: Customized Commerce 13 and earlier versions
Viewing all articles
Browse latest Browse all 9642

Setting CMS content for custom URL

$
0
0

Hi, 

I have a question related to custom URLs that I want to have a content. 

Basically, for pages in CMS, we extend PageController<TPage> where TPage : PageData

Then the said page comes to a controller in a method public async Task<ActionResult> Index(CustomPage currentContent) 

I'm wondering if we can have that CustomPage currentContent for URLs that don't match the CMS url? 

For example if page /Custom-Page1 is a content in CMS them the Index will have the currentContent set. But if I want /Custom-Page1-green and /Custom-Page2-blue then is no match and currentContent is null. Is there a way to load /Custom-Page1-green and /Custom-Page2-blue with the same currentContent as  /Custom-Page1?

I've tried to override IUrlResolverPipelineStep also IPageRouteHelper and IContentRouteHelper and yet something is not updating that currentContent. I tried IContentUrlResolver but I wasn't able to implement  it

Below are some of my attempts The code seems to work, but is executed after the Index method is called in both cases

public class UpdatedContentRouteHelper : IPageRouteHelper, IContentRouteHelper
{
    private readonly IHttpContextAccessor _httpContextAccessor;
    private readonly IContentLoader _contentLoader;
    public PageData ComputedPage { get; set; }
    public UpdatedContentRouteHelper(IHttpContextAccessor httpContextAccessor, IContentLoader contentLoader)
    {
        this._httpContextAccessor = httpContextAccessor;
        _contentLoader = contentLoader;
    }
    public PageReference PageLink => this.ContentLink.ToPageReference();
    public PageData Page => this.Content as PageData;
    public string LanguageID => this._httpContextAccessor.HttpContext?.Features.Get<IContentRouteFeature>()?.RoutedContentData?.RouteLanguage;
    public ContentReference ContentLink => ModifyContent(this._httpContextAccessor.HttpContext?.Features.Get<IContentRouteFeature>()?.RoutedContentData)?.ContentLink;
    public IContent Content => ModifyContent( this._httpContextAccessor.HttpContext?.Features.Get<IContentRouteFeature>()?.RoutedContentData);
    private IContent ModifyContent(ContentRouteData originalContent)
    {
        if (originalContent?.Content == null)
        {
            TransformUrl transformUrl = new TransformUrl();
            var originalUrl = _httpContextAccessor.HttpContext.Request.GetDisplayUrl();
            var pageId = TransformUrl.GetPageId(originalUrl);
            if (pageId != 0)
            {
                ComputedPage = _contentLoader.Get<CustomPage>(new ContentReference(pageId));
                return ComputedPage;
            }
        }
        return originalContent?.Content;
    }
}
public class CustomPipelineStep: IUrlResolverPipelineStep
{
    private readonly IContentLoader _contentLoader;
    private readonly IHttpContextAccessor _httpContextAccessor;
    public CustomPipelineStep(IContentLoader contentLoader, IHttpContextAccessor httpContextAccessor)
    {
        _contentLoader = contentLoader;
        _httpContextAccessor = httpContextAccessor;
    }
    public RoutingState Resolve(UrlResolverContext context, UrlResolverOptions options)
    {
        var originalUrl = _httpContextAccessor.HttpContext.Request.GetDisplayUrl();
        if (TransformUrl.IsUpdatedUrl(originalUrl))
        {
            var pageId = TransformUrl.GetPageId(originalUrl);
            if (pageId != 0)
            {
                var page = _contentLoader.Get<CustomPage>(new ContentReference(pageId));
                context.Content = page;
                return RoutingState.Continue;
            }
        }
        return RoutingState.Continue;
    }
}

My solution was to get the currentContent in the Index action based on the request URL, but I feel like some functionality is missing (for example Edit Page admin link when logged as administrator doesn't work but I expect other issues too)

(Not sure if it matters, but we want to do this to avoid having query strings and move them in the URL path to avoid some issues with encoding/special characters/improve SEO)


Viewing all articles
Browse latest Browse all 9642

Trending Articles