Page Not Found - Portal Engine

Suman Layek asked on May 26, 2016 19:22

How to forcefully redirect to the Page Not Found URL where Browser URL should not be changed. I need to create a same functionality as in Kentico but manually(through code). But I don't know how to do this. I have tried with the below codes but none of them are working and it throws Application_Error Exception in Kentico Event Log.I am looking for an option without Response.Redirect as we need to keep the URL for which page not found need to be shown.

public void RedirectToPageNotFound()
{
    System.Web.HttpContext.Current.Response.StatusCode = 404;
    throw new CMS.DocumentEngine.PageNotFoundException();
    Server.Transfer("~/404.aspx");
}

Below is the feature I want to implement I am using Kentico WildCard URL functionality to show data on page from custom table(EACH ROW HAS PAGE DATA LIKE TITLE, META DESC, BODYHTML ETC.) Now I want to redirect page to 404.aspx(Created on portal engine content tree) when there is no data found on the custom table.

Can you please suggest me on this issue.

Recent Answers


Trevor Fayas answered on May 26, 2016 20:43 (last edited on May 26, 2016 20:45)

If i understand, you want to load in a "page not found" at the current url (say /mydatapage?rowid=2)?

If so, i would instead use a custom macro to detect if the database object exists, then use that to make either the zone with the content appear, or if there isn't any object found, show another webpart zone that contains either some "404" message or a Page Placeholder with the path set to the 404 page.

1 votesVote for this answer Mark as a Correct answer

Suman Layek answered on May 26, 2016 23:31

Thank you Trevor for your reply but No, it is the same code alike Response.Redirect() but I already mentioned in my question that it should not redirect to 404 page in URL. URL should not show domainname.com/404.aspx instead it should be same URL with which you are trying to get the page. For your reference please go to this URL https://www.nicklauschildrens.org/health-information-library/XYZ. There is no page exists named XYZ (You can replace XYZ with any name) but still the page url not changed to link text but it is actually showing 404 page which I have created on content tree(Not a physical aspx file). The same behavior I would like to achieve through Kentico API code when I am using WILD CARD Url but no data found by query parameters.

0 votesVote for this answer Mark as a Correct answer

Virgil Carroll answered on May 26, 2016 23:48

Like Trevor I am a bit confused. If you want it to load the 404 page but show the XYZ url (even though there is no page at the XYZ address??) then you will need to still load up the 404 and rewrite the URL of that page to the XYZ URL. Basically you are creating an alternate URL for the 404 page on the fly. I would look into some of the API classes to look into how to rewrite these. You can also see how Kentico itself handles URL Aliases and override that process for the 404 page temporarily to display the URL you want it to display.

0 votesVote for this answer Mark as a Correct answer

Suman Layek answered on May 27, 2016 01:48 (last edited on May 27, 2016 01:50)

Hello Virgil, Thank you for the suggestion. As I have mentioned I am using Wild Card URL to display content on Page. As an example I have set page path to the below alias URL

/health-information-library/en/{section}/{typeofpage}/{aliasname}.

Based on the query string values I am displaying data on a page using query repeater where page content(HTML) fetched from a custom table. I am following WILD Card feature because there are thousands of records. Now my requirement is if the queryRepeater.items.count is 0 then show the 404 page without modifying the URL. I think it is more clear to you now.

0 votesVote for this answer Mark as a Correct answer

Virgil Carroll answered on May 27, 2016 03:43

Suman I believe I did understand that part, but my point is you are not going to have an easy time have the 404 page show up with the same URL as the visited one. You will either have to redirect to the 404 page and rewrite its URL to be the same as the page the person visited following your pattern: /health-information-library/en/{section}/{typeofpage}/{aliasname}.

OR

You should do what Trevor suggested and have a 404 web part at the URL path that only shows if no items are present or dynamically load a 404 web user control into the page when the URL has no items.

0 votesVote for this answer Mark as a Correct answer

Suman Layek answered on May 27, 2016 03:50

@Trevor - This is really nice tweak but I also logging page not found records on EventLog whenever the 404 occurred. So I must need to use the same functionality as in Kentico to log the error event automatically.

0 votesVote for this answer Mark as a Correct answer

Virgil Carroll answered on May 27, 2016 05:55

Suman, you could just add your code to show the web control in the method you have above, just not do the server redirect, but still throw the exception in the log...or just write directly to the log yourself.

0 votesVote for this answer Mark as a Correct answer

Suman Layek answered on May 27, 2016 20:13 (last edited on May 27, 2016 20:27)

For your reference please check the below code. It is a transformation of a query repeater which has a nested query repeater which is binding data based on data received fetched from database.

<cms:queryrepeater StopProcessing="false" runat="server" ID="qrArticle" TopN="1"/>
<script runat="server">
public string sectionAliasName { get; set; } 
public string articleAliasName { get; set; } 
public bool isArticle { get; set; } 

protected override void OnInit(EventArgs e)
{
      //Check from where page description will be fetched(Article/Category) 
      isArticle = Convert.ToBoolean(DataBinder.Eval(this.DataItem, "IsArticlePage"));

      //Add Page Title
      this.Page.Title =  Convert.ToString(DataBinder.Eval(this.DataItem, (isArticle == true ? "ArticleTitle" : "CategoryDisplayName" )));

      //Add Keywords Meta Tag
      if(!string.IsNullOrEmpty(Convert.ToString(DataBinder.Eval(this.DataItem, "MetaKeywords"))))
      {
        HtmlMeta keywords = new HtmlMeta();
        keywords.HttpEquiv = "keywords";
        keywords.Name = "keywords";
        keywords.Content = Convert.ToString(DataBinder.Eval(this.DataItem, "MetaKeywords"));
        this.Page.Header.Controls.Add(keywords);
      }

      //Add Description Meta Tag
      if(!string.IsNullOrEmpty(Convert.ToString(DataBinder.Eval(this.DataItem, "MetaDescription"))))
      {
        HtmlMeta description = new HtmlMeta();
        description.HttpEquiv = "description";
        description.Name = "description";
        description.Content = Convert.ToString(DataBinder.Eval(this.DataItem, "MetaDescription"));
        this.Page.Header.Controls.Add(description);
      }

      //Bind the repeater based on parameters
      sectionAliasName = Convert.ToString(DataBinder.Eval(this.DataItem, "SectionAliasName")); 
      articleAliasName = Convert.ToString(DataBinder.Eval(this.DataItem, (isArticle == true ? "ArticleAliasName" : "CategoryAliasName" )));

      qrArticle.WhereCondition= "SectionAliasName = '" + sectionAliasName + "' AND ISNULL(ArticleAliasName, CategoryAliasName) = '" + articleAliasName + "'";
      qrArticle.WhereCondition= "SectionAliasName = '" + sectionAliasName + "' AND (ArticleAliasName = '" + articleAliasName + "' OR CategoryAliasName='" + articleAliasName + "')";
      qrArticle.QueryName="NCH.Transformations.GetCategoryArticles";
      qrArticle.OrderBy = "IsArticlePage";
      qrArticle.TransformationName= (isArticle==true ? "NCH.Transformations.GetArticleDescriptionDetail" : "NCH.Transformations.GetCategoryDescriptionDetail" );
      if(qrArticle.Items.Count == 0)
      {
             // Show Page Not Found
            //Response.Code = 404; //This line logging a 404 record on Event Log but it is
                                   // causing a Application Exception also.
      }
      base.OnInit(e);
}
</script>
0 votesVote for this answer Mark as a Correct answer

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