How to render custom 404 page by code behind?

Hanh Dang asked on September 22, 2017 06:42

Hi all,

I am looking for an method to render custom 404 page manually without changing the URL as Kentico does.

For example: I have a page with url /cars/{car_id}. The web part will gets car_id from url, then finds the car in custom table base on the id. In case of no car found, it should load the content of 404 page.

Here is the code:

// Prepares the code name of custom table
string carClassName = "abc.cars";

// Get car id from url
int carId = ValidationHelper.GetInteger(Request.QueryString["car_id"], 0);

// Gets the custom table
DataClassInfo carTable = DataClassInfoProvider.GetDataClassInfo(carClassName);
if (carTable != null)
{
    // Gets a custom table record with a car id
    CustomTableItem car = CustomTableItemProvider.GetItem(carId, carClassName);

    if(car != null)
    {
        // Code to display car's data
        // ...
    }
    else 
    {
        // Code to render custom 404 page

        // I have tried some ways but they didn't work:

        // ----1----        
        // Response.StatusCode = 404;

        // This throws an exception with the message:
        // "Only one page manager (CMSPageManager or CMSPortalManager) is allowed on the page."



        // ----2----        
        // throw new HttpException(404, "Error");

        // This just shows a empty page in browser
    }
}

Do you have any idea?

Best regards,

Hanh Dang

Recent Answers


David te Kloese answered on September 22, 2017 10:39

Hi,

Have you've seen my blog about the "best 404 page ever". I'm doing something similar: http://kenticoblog.evident.nl/dtk/october-2016/the-best-404-error-page-ever

By setting your custom 404 page in the Settings> Content > Page not found > Page not found URL you don't have to send headers yourself. My URL stays the same as you can test on my blog.

1 votesVote for this answer Mark as a Correct answer

Chetan Sharma answered on September 22, 2017 12:20

I ran into this kind of problem when wanted to show 404 page for items that disabled false in the Custom modules. I created a small web part that can be connected to a datasource. If your datasource return no records based on your dynamic parameters, my web part will redirect a user to a 404 page.

Does that helps you?

Chetan!!

0 votesVote for this answer Mark as a Correct answer

Hanh Dang answered on September 25, 2017 04:26

Hi David and Chetan, thank you for your replies

@David: There is no individual page for car. I have created a page and changed its url as a wildcard url to show car's data. CMS can still return that page even the car id in url is incorrect. So, 404 header will not be sent since no car found.

@Chetan: I think about this way before but I don't think it is best choice for some reasons:

  • It take 2 time to redirect to 404 page
  • It could be confused because sometime it just renders 404 page without change the url, sometime it redirects to the 404 page. Client will not be happy about that.
0 votesVote for this answer Mark as a Correct answer

Zach L answered on October 17, 2017 16:20

Just bumping this up, I've come across the same issue and would be great if someone has found a solution for this. David's solution doesn't do what me and Hanh seems to need.

The page this needs to work on exists in Kentico it's just that under certain circumstances it needs to: 1. Return a 404 status code 2. Show the contents of a 404 page 3. Perform no redirects to that 404 page such that the url entered stays the same.

@Hanh Dang Did you manage to solve this issue?

0 votesVote for this answer Mark as a Correct answer

Hanh Dang answered on October 18, 2017 05:41 (last edited on October 18, 2017 05:42)

Still not, but you can try this way

  • In code behind, throw an 404 exception
  • In web.config, add a httpErrors for 404 response

The problem is:

  • It will redirects/rewrites all 404 response to the path in httpErrors. It affects to the whole website, so 404 setting in CMS will not works anymore.
  • Status code of 404 page is 200, not 404
0 votesVote for this answer Mark as a Correct answer

Sébastien Gumy answered on January 8, 2018 11:01

Hi,

I had the same issue, but I managed to solve it with the following lines (Kentico 10) :

PortalContext.Clear();
CMSHttpContext.Current.Response.StatusCode = 404;
URLRewriter.RewriteUrl(RequestStatusEnum.PageNotFound, string.Empty, ExcludedSystemEnum.Unknown);

I'm not sure if it's a clean way, but it seems to work.

Best regards

4 votesVote for this answer Mark as a Correct answer

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