I have an ActionResult method where I query a database for products and if none are found, I redirect the user to the page /products/noproductsfound.
noproductsfound currently exists as a node in my backoffice and it has the Id of 1093. Currently, I am just doing:
return RedirectToUmbracoPage(1093);
What happens if:
The page (node) is deleted?
What is the best way to store the node id to be used in RedirectToUmbracoPage? AppSettings?, etc
Is this the proper way to redirect to another page? Why isn't there an option to redirect with the name of a page instead?
I tried doing return View("NoProductsFound"), but this is done after a post, so the view gets rendered, but stays on the same page, so a refresh keeps posting data.
If page nodes can be stored in AppSettings and the page is deleted and recreated assuming with a different Id, a non-technical person is not going to want to update AppSettings with the new Id, so how is this handled?
One way to go is to create a content picker where you can choose where to redirect to and maybe default to Home if you aren't able to get the selected node by id. That's probably the way I'd do it. I'd have a picker that said, "Products Not Found Page", and I would try to redirect to that.
Is there is some sort of content structure you can use? For example, is the "No Products Found" page the child of the current page? Or does it have a specific document type? if so, you might be able to use that to determine what page to redirect to.
The reason that redirecting by name is a bad idea and isn't supported is that the client will inevitably change the name of the node. You'd run into the same problem you're having now, except that it would happen when someone changed the node name and not when the node was deleted.
They start off on a page that is locate at /products/search
There is a form on this page and when they click search, it performs a post, queries a database. If records are found, I am doing return View("products"), not sure if this is a good idea because the user stays on /products/search with the rendered products.cshtml view (care to chime in here as well). If no products are found, the user is redirected to /products/noproductsfound
The content structure is as follows:
The root node is Home
Under Home, I have Products
Under Products, I have Product, NoProductsFound, Search
Since you brought it up, do you think it would be a good idea to have a property for redirecting the user to a productsfound node and a products not found node? This way I can avoid reposting when the user clicks refresh?
If I do a content picker, how is this called from the backend (code) in my action method?
Also, on my Search.cshtml, the form is rendered via a partial called ProductFilter.cshtml. This is the controller that is performing the action to search, so what document type would by content picker go on.
What I sometimes do is crawl from the current page up the content tree to the home node. Then, I descend down the tree looking for the node with the correct doctype. Once I find it, I cache the node ID in memory. If that is invalid for some reason (e.g., if the user deletes and recreates the node), there may be errors, but they'll be fixed once the application pool restarts.
I have also created more sophisticated code that stores the node in a setting in the content tree (typically, using a content picker). There are various ways I have stored settings (e.g., property on the home node, property on a descendant node, child nodes of the home node, under a different global node section).
I sometimes also have fallback strategies. For example, if I can't find a configured search node, I'll then fallback to attempting to find it using the home descending doctype technique.
There's no real right way of doing it, but I definitely wouldn't hard code a node ID, as that's not easy for non-developers to fix. The above techniques have various pros and cons you'll have to consider per-site. For example, some sites I build don't have different doctypes (for the most part). And other sites don't have a settings system. And others are too large to do the descendant crawl technique.
Best practice for redirecting to Umbraco page?
I have an
ActionResult method
where I query a database for products and if none are found, I redirect the user to the page/products/noproductsfound
.noproductsfound
currently exists as a node in my backoffice and it has theId of 1093
. Currently, I am just doing:return RedirectToUmbracoPage(1093);
What happens if:
View("NoProductsFound")
, but this is done after a post, so the view gets rendered, but stays on the same page, so a refresh keeps posting data.One way to go is to create a content picker where you can choose where to redirect to and maybe default to Home if you aren't able to get the selected node by id. That's probably the way I'd do it. I'd have a picker that said, "Products Not Found Page", and I would try to redirect to that.
Is there is some sort of content structure you can use? For example, is the "No Products Found" page the child of the current page? Or does it have a specific document type? if so, you might be able to use that to determine what page to redirect to.
The reason that redirecting by name is a bad idea and isn't supported is that the client will inevitably change the name of the node. You'd run into the same problem you're having now, except that it would happen when someone changed the node name and not when the node was deleted.
Let me know if that helps.
Hi Mark,
So this is the process the user goes through:
They start off on a page that is locate at
/products/search
There is a form on this page and when they click search, it performs a post, queries a database. If records are found, I am doing
return View("products")
, not sure if this is a good idea because the user stays on/products/search
with the renderedproducts.cshtml
view (care to chime in here as well). If no products are found, the user is redirected to/products/noproductsfound
The content structure is as follows:
Since you brought it up, do you think it would be a good idea to have a property for redirecting the user to a productsfound node and a products not found node? This way I can avoid reposting when the user clicks refresh?
If I do a content picker, how is this called from the backend (code) in my action method?
Also, on my Search.cshtml, the form is rendered via a partial called ProductFilter.cshtml. This is the controller that is performing the action to search, so what document type would by content picker go on.
Thanks for the help Saied
Hi Mark,
I added this on my Search DocumentType:
Does the above look like a good way to do it?
If the Umbraco page I want to redirect to needs a custom model, what method would I use?
What I sometimes do is crawl from the current page up the content tree to the home node. Then, I descend down the tree looking for the node with the correct doctype. Once I find it, I cache the node ID in memory. If that is invalid for some reason (e.g., if the user deletes and recreates the node), there may be errors, but they'll be fixed once the application pool restarts.
I have also created more sophisticated code that stores the node in a setting in the content tree (typically, using a content picker). There are various ways I have stored settings (e.g., property on the home node, property on a descendant node, child nodes of the home node, under a different global node section).
I sometimes also have fallback strategies. For example, if I can't find a configured search node, I'll then fallback to attempting to find it using the home descending doctype technique.
There's no real right way of doing it, but I definitely wouldn't hard code a node ID, as that's not easy for non-developers to fix. The above techniques have various pros and cons you'll have to consider per-site. For example, some sites I build don't have different doctypes (for the most part). And other sites don't have a settings system. And others are too large to do the descendant crawl technique.
is working on a reply...