5

I have a component on my page that is a controller rendering and I'd like to pass a parameter in through the URL, but not as a query string parameter. So, instead of site.com/product?id=123, I want: site.com/product/123

/product is a valid page with a layout and renderings, but when I try to go to /product/123, I naturally get "document not found".

Do I just need to add a custom route for this page in the initialize pipeline?

I should know this, but, alas, I don't.

4 Answers 4

6

Rather than an item named product, create an item named products that represents the entire collection, and then create a child item named * to represent any individual product.

Wildcard items will be resolved with any path that doesn't match an existing item, so if your tree is:

/products
/products/example
/products/*

A request to http://example.com/products/example will get you the /products/example item, whereas a request to http://example.com/products/123 will get you the /products/* item.

With your items named appropriately, it then makes sense that a request to http://example.com/products would return some sort of collection view of all products, whereas http://example.com/products/123 would return the details for the product with an ID of 123.

It will be up to you to parse the current URL for the ID of the product. Additionally, you will need to manually handle 404 errors, as Sitecore can't tell whether or not a given path is meant to exist.

As far as assigning renderings, presentation details set on the * item will be applied to the given request.

3
  • 1
    All of these approaches are valid I believe, but this is by far the easiest, as long as all of the children using the wildcard use the same presentation. Commented Oct 7, 2016 at 17:50
  • @LonghornTaco, and if they do need different presentations, it's typically indicative that the items are not the same type and should be reorganized in some other fashion, such as by putting them in subcategories such as /products/widgets/* and /products/doodads/*.
    – zzzzBov
    Commented Oct 7, 2016 at 17:55
  • @zzzzBow Exactly what I was thinking Commented Oct 7, 2016 at 18:21
7

I've done this before using a wildcard item for the parameter and a custom resolver processor that runs just after the ItemResolver - The ItemResolver should resolve to /sitecore/content/mysite/products/* - so in my custom resolver I know if I'm on that item, parse the Url, take the last segment and store the value somewhere - like Sitecore.Context.Items["productId"] - then use that in the controller action.

Once that is done, set Sitecore.Context.Item to the parent, so you get the presentation for the Products item.

Example controller action then:

public ActionResult Index(string id)
{
    var productId = Sitecore.Context.Items["productId"].ToString();
    // Do something with that here...
}
3

How about using just a wildcard? Assuming all instances of /product/xxx use the same layout, you'd create a single subpage under it called * and set the renderigns on that item. Then in your Controller Rendering, you'll need to read the product ID off of the the url. Something like:

var productID = Sitecore.Web.WebUtil.GetUrlName(0);

3

Longhorn I've handled this by doing a url re-write in IIS to convert your request for a SEO Friendly url to a parameter based one like so:

<rule name="Friendly Product Urls">
    <match url="^(.*)products/(product)/([0-9]+)" />
    <action type="Rewrite" url="{R:1}products/product?id={R:3}" />
</rule>

My controller action then picks this up fine:

public ActionResult Index(string id)
{
}

Would this work for you? I'd be interested in how others have solved this though.

4
  • I'd rather not have to handle this at the web server level if I don't have to. Commented Oct 7, 2016 at 16:30
  • agreed, I'm sure there are better ways. What version of Sitecore are you using? Commented Oct 7, 2016 at 16:32
  • We're using 8.1-update 3 Commented Oct 7, 2016 at 16:33
  • @LonghornTaco In that case I believe you'l need this module for the wildcard options discussed here to work won't you? marketplace.sitecore.net/Modules/W/… Commented Oct 7, 2016 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.