Prevent product to redicret to base url?

So i have written this beautiful piece of code :slight_smile: this were supuse to send all requests with a json prefix to our Json action in the product controller insted of the normal product action/view.

public class RoutingServiceDecorator : RoutingService
........
public override bool TryGet([NotNull] AssortmentRouteLookupInfo lookupInfo, out RouteInfo routeInfo)
	{

            //When we get request to litium we inject our magic
			bool result = false;

			if (lookupInfo.Segments.Length > 1 && lookupInfo.Segments[0] == "json")
			{
				//Remove the json slug from the request
				var defaultSegments = lookupInfo.Segments.Slice(1);

				//lookupInfo is read only, so we need to recreate it to alter the url segments
				lookupInfo = new AssortmentRouteLookupInfo(
					lookupInfo.CultureInfo,
					defaultSegments,
					lookupInfo.WebsiteSystemId,
					lookupInfo.AssortmentSystemId,
					lookupInfo.ChannelSystemId,
					lookupInfo.UseHistory,
					lookupInfo.DisplayOnWebsiteSystemId);

				//Make a lookup on the base url
				result = _parent.TryGet(lookupInfo, out routeInfo);
				//If we have a hit we replace the controller action
				if (result)
				{
					// eg: "~/MVC:Litium.Accelerator.Mvc.Controllers.Product.ProductController, Litium.Accelerator.Mvc:ProductWithVariants";
					var templateParts = routeInfo.TemplateFileName.Split(',');

					//Make sure we have a default controller action selected
					if (templateParts.Length > 1)
					{
						var controller = templateParts[0].Trim();  // eg: "~/MVC:Litium.Accelerator.Mvc.Controllers.Product.ProductController
						var action = templateParts[1].Trim(); // eg: Litium.Accelerator.Mvc:ProductWithVariants

						//Replace the template action to use the injected one
						routeInfo.TemplateFileName = routeInfo.TemplateFileName.Replace(action, "Litium.Accelerator.Mvc:Json");
					}
				}

				return result;
			}
			else
			{
				//Do the normal lookup
				return _parent.TryGet(lookupInfo, out routeInfo);
			}

		}

This whould work like magic, BUT what happens, is that before we reach the Json action in the product controller, litium looks up the real url, and do a 301 redireckt to so we always end up in the “real” action.

Can I somehow disable this redirect from code? or must i just call the product controller directly from here?

Litium version: 7.4

Is the url-format important with the prefix or can you try to use /the/category/and/product.json instead? that should directly map the request to the json action on the controller without any extra steps.

Otherwise, disable the URL check is made on the RouteRequestLookupInfo.SkipUrlCheck that not is passed into the class that you are decorating. Instead you need to decorate the Litium.Web.Routing.PageRouteResolver, Litium.Web.Application that is the entry point for all url lookups.

1 Like

This is what I get for not lerning the c#/mvc from the bottom up :smiley: Spendning 2 h solveing a non existing problem :smiley: Thanks!

This is a Litium feature to be able to access the different action methods on the controller and still maintain the created url. Otherwise the controller is registered as default with /controller/action/id but that’s not working (what I know) with dynamic generated links.

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