Getting page by url

Litium version: [7]

Hi!

I’m new to Litium and trying to achieve some basic things programatically. For example getting a page using a given url. I am using Litium 7. Using this line of code
var page = _pageService.Get(new Guid(id));
I can get the page through the id but how can I get the page (or the Id of the page) based on an url?

My guess whould be to use the DataService, somethng like

using (var query = _dataService.CreateQuery<Page>())
            {
                query.Filter(x => x.Field("_url", "eq", slug));
                var pages = query .ToList();
            }

But this uses the slug and not the full url categorys on lets say a product.

Thank you for your reply!

For some reason I always get the value null back when using that piece of code. :frowning:

Are you sending in the entire URL or just the slug for the page?

https://www.example.com/customer-service/about-us - only about-us would be the actual URL for the page.

In a standard Accelarator, the URL is also multi-culture so you need to pass the culture as well.

query.Filter(x => x.Field("_url", "eq", CultureInfo.CurrentUICulture, slug));

But why do you want to do a reverse lookup on the URL? Maybe there’s another way to accomplish what you’re looking for.

Data service hits the database directly, so you might have to cache the result depending on when it’s used. It should also be possible to use Lucene search to find the page.

Yes, the most preforment why i know of whould be to add the full main url to the Lucene/elastic index

It exists several options to find the page based on url-slug but it’s depend on what the purpose why we need to find the page by the url what method that suits the need best.

Hi,

sorry for the late reply! I’m playing around with the possibility to build an spa and keeping the url structure so I guess the preferred way should be to get the page through the entire URL. But maybe there is a better way to achieve this?

In the project I’m working on this does not work either:

query.Filter(x => x.Field("_url", "eq", CultureInfo.CurrentUICulture, slug));

maybe I’m doing something wrong, I guess I have to investigate further.

the url on each product is only the slug part for that page (the page url, not the complete url) so this is not working and using the dataservice for each call will not be good for performance.

This is now an internal API that may change in future but is the way that Litium it’self doing the url-lookups.

Inject the Litium.Web.Routing.PageRouteResolver and use the TryGet-method, the method return true/false depending on if the url exists and the out-parameter Litium.Web.Routing.RouteRequestInfo contains the data about the found page/category/product.

If the Litium.Web.Routing.RouteRequestInfo.Data is of the type Litium.Web.Products.Routing.ProductPageData the url will correspond to a category or product page.

Thank you Patric! Now I get the idea :slight_smile:

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.