Try our conversational search powered by Generative AI!

404 page to trigger for page StopPublished and in WasteBasket

Vote:
 

Hi all!

Iam trying to make our 404 page to trigger when a visitor follows a link to a page that is stop published or in the waste basket, instead of an EpiServer login page showing up.

 

First, please, lets cover the StopPublished case. Iam following this guide:
blog.mathiaskunto.com/2012/04/03/think-things-through-or-simple-way-to-404-not-found-instead-of-episerver-login-screen-for-unpublished-pages/

Doing it like him/her generated all sorts of errors in my project (because I dont have enough knowledge curtainly). However, I did it like this, just added the overide method in the code behind for StandardPage page template:
public override void AccessDenied()
{
            if (CurrentPage.StopPublish <= DateTime.Now)
            {
                Response.Redirect("http://www.ourSite.se/Error.htm");
            }
            base.AccessDenied();

}

It did work! First question: Is this ok? I understand that I have to do this in the codebehind for our other page templates, like newsPage and so forth ... But otherwise, its ok?


Ok. Now to the case where the visitor clicks on a link that points to page in the waste basket. How do I accomplish the same thing for that scenario? I tried if (CurrentPage.IsDeleted) but that doesnt work, I guess it triggers if the page is deleted from the waste basket, correct?

 

It would mean so much to get this working! Thanks in advance for your answers!

#59115
May 22, 2012 13:43
Vote:
 

My bad, it did work with IsDeleted ... I must have got the code in the wrong order or something. It works when if IsDeleted is alone. How would you write it?

#59116
May 22, 2012 14:06
Vote:
 

This did work

public override void AccessDenied()
        {
            if (CurrentPage.IsDeleted)
            {
                Response.Redirect("http://www.ourSite.se/Error.htm");
            }
            
            if (CurrentPage.StopPublish <= DateTime.Now)
            {
                Response.Redirect("http://www.ourSite.se/Error.htm");
            }

            base.AccessDenied();
        }

 

But my question sort of stands: this is "solid" so to speak? Just do it in the code behind for all page templates?

#59117
May 22, 2012 14:14
Vote:
 

Hi,

You could do a:

Response.StatusCode = (int)HttpStatusCode.NotFound;
Response.Status = "404 Not Found";
Response.End();

Instead of a Response.Redirect() and rely on the built-in 404 handling in asp.net.   

#59118
May 22, 2012 15:00
Vote:
 

We usually have a TemplateBase that we inherit from, and then you only have to do it in one file. (TemplateBase inherits from TemplatePage, and all template inherits from TemplateBase. In TemplateBase you add the code you've written)

You should also do a 301-redirect (permanent redirect), then all robots will understand that the page is missing.

In .NET4 there is a Response.RedirectPermanent(...), otherwise you can use the following extensionmethod:

public static void Redirect301(this HttpResponse response, string url)
        {
            response.Status = "301 Moved Permanently";
            response.AddHeader("Location", url);
        }

    

#59119
May 22, 2012 15:00
Vote:
 

I don't know which status code is most suitable, maybe a "410 Gone" (http://en.wikipedia.org/wiki/List_of_HTTP_status_codes#410). But if you already have a 404-page, it's easier to just send the visitor there by giving them a 404.

The 301 states that the page has moved but still is available on another location, which is not the case. It is not longer available.

#59120
Edited, May 22, 2012 15:50
Vote:
 

As you say it may be better to just give them the 404 code. I guess a 301 redirect to a 404 page will work in the same way, but with an extra step.. :)

#59121
May 22, 2012 15:52
Vote:
 

True that :)

#59122
May 22, 2012 15:53
Vote:
 

"Response.StatusCode = (int)HttpStatusCode.NotFound;
2    Response.Status = "404 Not Found";
3    Response.End();
Instead of a Response.Redirect() and rely on the built-in 404 handling in asp.net."

I tried it, but it didnt work. It is something fishy with the error handling in our IIS.

I look in to the other bits tomorrow.

Thank you very much!!

#59124
May 22, 2012 16:05
Vote:
 

Do you have a working 404 page already?

#59125
May 22, 2012 16:06
Vote:
 

Johan: Yes. In the IIS we point to a html file (/Error.htm), in the html file is a JavaScript that redirects to a page in Epi (/Om-webbplatsen/404). We tried to redirect direct to /Om-webbplatsen/404 (of course) but it didnt work.

#59126
May 22, 2012 16:10
Vote:
 

If the site is running on a IIS7.5 you can do this:

<system.webServer>
	<httpErrors errorMode="Custom">
		<remove statusCode="404" />
		<error statusCode="404" path="/notfound.aspx" responseMode="ExecuteURL" />
		<remove statusCode="500" />
		<error statusCode="500" path="/error.aspx" responseMode="ExecuteURL" />
	</httpErrors>
</system.webServer>

 And in notfound.aspx you can load your page (/Om-webbplatsen/404/) by just setting the CurrentPage property to a page which the editor can change on the startpage.

protected override void OnPreInit(EventArgs e)
{
    base.OnPreInit(e);


    CurrentPage = DataFactory.Instance.GetPage((PageReference)startPage["NotFoundPage"]);
}

    

#59127
May 22, 2012 16:15
Vote:
 

We have IIS 6, sorry. But it is nice to know anyway.

Iam working on a friendly asp.net crasch (yellow screen of death) page as well. This works. Looks ok?

In web.config:

<!-- turn off EpiServers friendly asp error page -->
<siteSettings globalErrorHandling="Off" alot of other attributes/>

<!-- trigger our own friendly asp error page for remote requests -->
<customErrors mode="RemoteOnly" defaultRedirect="~/Templates/Public/Pages/ASPerror.aspx"/>

#59128
May 22, 2012 16:23
Vote:
 

Erik: Can you please explain a little regarding your post (fifth one)?

- Where do I edit TemplateBase? I can create one, like this?
public class MyPageBase : EPiServer.TemplatePage

- In the class file I put the code I used (the overide method for AccessDenied)?

- The 301 redirect you mention, do I put it in the same class file? Just like you written it?

Sorry if this is newbie'ish =)

#59149
May 23, 2012 10:06
Vote:
 

Yes, and then all your page templates (like StandardPage) needs to inherit from TemplateBase instead of TemplatePage. :)

You can skip the extensionmethod and do it directly where you Redirect by just doing:

Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", url);

    

#59150
May 23, 2012 10:09
Vote:
 

Now my method looks like this (stil testing in the standardPage template)

if (CurrentPage.StopPublish <= DateTime.Now)
            {
                Response.Status = "301 Moved Permanently";
                Response.AddHeader("Location", "/Error.htm");
            }

It doesnt work, sorry, just a login page. I also tried an absolute url "http://OurSite.se/Error.htm"

Something wrong?

#59151
May 23, 2012 10:22
Vote:
 

Are you sure you ever enter that method and if-statement? Dows it work with an ordinary Response.Redirect?

#59153
May 23, 2012 10:26
Vote:
 

Yes it works fine with just this code (and I tried both to stopPublish the link target page and moved it to waste basket - it triggers fine for both scenarios). The method as it is now (your new code commented out at the moment):

public override void AccessDenied()
        {
            if (CurrentPage.IsDeleted)
            {
                Response.Redirect("http://OurDEVSitesIP/Error.htm");
                //Response.Status = "404 Not Found";
                //Response.AddHeader("Location", "/Error.htm");
            }
            
            if (CurrentPage.StopPublish <= DateTime.Now)
            {
                Response.Redirect("http://OurDEVSitesIP/Error.htm");
                //Response.Status = "404 Not Found";
                //Response.AddHeader("Location", "/Error.htm");
            }

            base.AccessDenied();
        }

#59154
Edited, May 23, 2012 10:32
Vote:
 

OK, that is with the 404 code, but I guess you've tried the 301 aswell. Not sure why it's not working. Try to add a Response.End() after maybe? :)

#59155
May 23, 2012 10:37
Vote:
 

Ok cool. I figure it out now. Thank you both. Great help. Have a really nice day and enjoy the weather. =)

#59156
May 23, 2012 10:38
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.