Get the current category from request

Vote:
 

Hi,

I need to find which category is current being visited. We have one specific category that "represents" another site and theme.

I'm not that strong with the Commerce API, yet.

What I have so far is this action filter attribute:

public class UniverseFilterAttribute : ActionFilterAttribute
{
	public override void OnActionExecuting(ActionExecutingContext filterContext)
	{
		PageRouteHelper pageRouteHelper = ServiceLocator.Current.GetInstance();
		PageData currentPage = pageRouteHelper.Page;
		// TODO
	}
}

Thanks.

#172412
Edited, Dec 02, 2016 9:16
Vote:
 

Hi Kristian,

You could try this:

    public class UniverseFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            var referenceConverter = ServiceLocator.Current.GetInstance<ReferenceConverter>();
            var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
            var routeHelper = ServiceLocator.Current.GetInstance<IContentRouteHelper>();


            var content = routeHelper.Content;
            if (content == null)
            {
                return;
            }

            // Getting catalog content type
            var catalogContentBase = content as CatalogContentBase;
            if (catalogContentBase != null)
            {
                // now we have catalogContent.CatalogId
                // and we can find catalog from this CatalogId

                var catalogLink = referenceConverter.GetContentLink(catalogContentBase.CatalogId, CatalogContentType.Catalog, 0);
                var catalogContent = contentRepository.Get<CatalogContent>(catalogLink);

                // now we have catalogContent
            }
        }
    }

We have catalogContent now :)

Best regards,

/Son Do

#172414
Dec 02, 2016 10:58
Vote:
 

Hi,

I think 

var routeHelper = ServiceLocator.Current.GetInstance<IContentRouteHelper>();


var content = routeHelper.Content;
if (content == null)
{
return;
}

is what you need. content here is the content being visited, so if you compare it to the special category (I'm not sure if you meant it's catalog or node), then you can do special processing. The easiest way should be compare the content.ContentLink with the special category contentLink. (A common approach would be store that ContentLink as a property in StartPage.

Regards,

/Q

#172416
Dec 02, 2016 11:13
Vote:
 

@Quan Mai,

Yes, it was just what I needed. I ended up with this code - thanks for your help.

public class UniverseFilterAttribute : ActionFilterAttribute
{
	public override void OnActionExecuting(ActionExecutingContext filterContext)
	{
		Controller currentController = filterContext.Controller.ControllerContext.Controller as Controller;
		if (currentController == null)
		{
			return;
		}

		var routeHelper = ServiceLocator.Current.GetInstance<ContentRouteHelper>();

		var content = routeHelper.Content;
		if (content == null)
		{
			return;
		}

		var catalogContentBase = content as CatalogContentBase;
		if (catalogContentBase == null)
		{
			return;
		}
		
		// My implementation went here.
	}
}
#172478
Edited, Dec 05, 2016 8:50
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.