Get Category Url

Hi,

I’m finding trouble to get URL for specific Category.
I have a block that will display n categories that then will link to the category page. I found two ways to achieve almost what i want.

In my ViewModel i have:

var category = model.Fields.GetValue<Guid>BlockFieldNameConstants.LinkToCategory).MapTo<Category>();

I found that there are these extensions =>

using Litium.Foundation.Modules.ExtensionMethods;
using Litium.Accelerator.Extensions;
...
var href = category.GetUrl(CultureInfo.CurrentUICulture)

Gives me url for respective category eg:
category-1

but no path that i could use.

Another way =>
Inject and use in the builder.

Litium.Web.UrlService _urlService;
...
var href = _urlService.GetUrl(category.Category);

Gives me an long id string when in backoffice, and null otherwise
“/!2726b040-9c16-47de-a701-e30d02727e69!1!00000000-0000-0000-0000-000000000000!0!0/00000000-0000-0000-0000-000000000000/0aee0775-5699-4670-9826-1a53d388c3e1”

Litium version: 7.1

You need to pass the second param, which is a CategoryUrlArgs. Its property ChannelSystemId has to have value, otherwise null will be returned. If the Category is not activated in that channel, null will also be returned.

Hi, thank you for the response.
Could you please specify a bit?

RequestModelAccessor requestModelAccessor injection in the builder and tried to send i

We tried with:

_urlService.GetUrl(category.Category, new CategoryUrlArgs(_requestModelAccessor.RequestModel.ChannelModel.Channel.SystemId))

_urlService.GetUrl(category.Category, new CategoryUrlArgs(_requestModelAccessor.RequestModel.ChannelModel.SystemId))

Thanks again.

That seems to be correct. You still got null as the result? Is the category activated on current channel, and be published? What Url does it have?

It works now, seems it’s an db issue.

Had issues that a category was only available through back-office links and same with the link to an category.

We checked following settings

  • it was published on all channels.
  • it had an url
  • assortment was set

As soon as we tested with another db it worked.

1 Like