Custom 404 page through code behind

Zach L asked on October 17, 2017 17:17

Hi all, I've come across a bit of an issue, I need to find a way of showing a 404 page but on a page that does exists in the CMS it's just that under certain circumstances it needs to: 1. Return a 404 status code 2. Show the contents of a custom 404 page 3. Perform no redirects to that 404 page such that the url entered on the initial request stays the same.

In one of the web parts on that page I perform a check, if it's true a 404 page needs to be shown instead of the page's usual content. In Kentico 9 this was possible with

CMS.URLRewritingEngine.URLRewriter.PageNotFound(alwaysRedirect: true);

However in Kentico 10 this was removed as it "was never intended for public use."

Any help would be greatly appreciated!

Recent Answers


Peter Mogilnitski answered on October 17, 2017 18:07 (last edited on October 17, 2017 18:12)

There is a custom response web part, adding this web part to a page causes it to return a custom HTTP response when the page is requested. The response is only generated on the live site. You can add it to your template, set response to 404 and enable it based on a macro("certain circumstances").

0 votesVote for this answer Mark as a Correct answer

Zach L answered on October 17, 2017 18:40

Unfortunately this gives me an error : "Only one page manager (CMSPageManager or CMSPortalManager) is allowed on the page.". I have tried a similar approach by doing a Server.TransferRequest to a "Page-not-found" cms page with a custom web part that simply had a Response.StatusCode = 404 on it but that also gives me the same error.

0 votesVote for this answer Mark as a Correct answer

Peter Mogilnitski answered on October 17, 2017 19:06 (last edited on October 17, 2017 19:12)

Are you using ASPX mode or portal Mode? It looks like you have conflicting methods, i.e. if you are lets say in the portal mode but in your custom web part you call methods from ASPX mode, i.e. something like <cms:CMSPageManager ID="CMSPageManager1" runat="server" />. Custom response web part web part will work in the portal mode. Leave custom response web part in your template and try to narrow down a problematic web part, by removing web parts one by one and checking if 404 is working (You can always do undo checkout to restore your template).

1 votesVote for this answer Mark as a Correct answer

Matt Nield answered on October 17, 2017 23:09 (last edited on October 17, 2017 23:10)

Zach, I'm trying this on a Vanilla Kentico 10 install. The Custom resonse web part looks to be the right direction, but I am experiencing the same issue as you with the page manager issue. I've tried two scenarios in a clean install of Kentico 10 and am using Portal.

  1. Create a new page named Test page in the root of my site with an ad-hoc template. Create a new page called Page not found in the site root with a simple template and some wonderful copy. In Settings > Content > Page not found URL, set the value ~/page-not-found.aspx. Edit the template of Test page to include a Custom respons web part with a status code of 404. When I navigate to test-page.aspx, I get the error you have.
  2. (assume we're srating again) Create a new page named Test page in the root of my site with an ad-hoc template. Create a new web form in the CMSPages folder called PageNotFound.aspx with some wonderful copy. In Settings > Content > Page not found URL, set the value ~/CMSPages/PageNotFound.aspx. Edit the template of Test page to include a Custom respons web part with a status code of 404. When I navigate to test-page.aspx, I get the content of my ASPX error page.

Pretty odd, I've done nothing custom for the page managers, it's a complete clean install. Based on that, I'd be tempted to raise a suport ticket with support@kentico.com to determins why this message is appearing, as the theory to me is sound.

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.