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?
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?
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.
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);
}
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.
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.. :)
"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!!
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.
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"]);
}
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"/>
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 =)
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);
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?
Are you sure you ever enter that method and if-statement? Dows it work with an ordinary Response.Redirect?
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();
}
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? :)
Ok cool. I figure it out now. Thank you both. Great help. Have a really nice day and enjoy the weather. =)
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!