How to custom a friendly 404 error

Vote:
 

Hello guys,

I'm trying to figure out how to do a friendly 404 error. My project using epi cms 9.12 and IIS 6. I'm appreciate your help in advanced! Thanks!

#173870
Jan 11, 2017 15:03
Vote:
 

There are a number of ways to create an editable 404 message. I've bee using the current solution for some time know. Based on this old post: http://world.episerver.com/Forum/Developer-forum/EPiServer-7-CMS/Thread-Container/2013/3/Custom-404-page-in-EPi-7-MVC/

Web.config

<system.webServer> 
  <httpErrors errorMode="Custom" existingResponse="Auto">
      <remove statusCode="404" />
      <error statusCode="404" responseMode="ExecuteURL" prefixLanguageFilePath="en" path="/NotFound" />
  </httpErrors>
....
<system.web>
  <customErrors mode="On">
      <error statusCode="404" redirect="/NotFound"/>
  </customErrors>
...

Controller

Add this Method to your "default" page controller.

public ActionResult NotFound(PageData currentPage)
        {
            var startPage = ContentReference.StartPage.Get<StartPage>(); 
            // Set RoutingConstants.NodeKey so EPi's extension method RequestContext.GetContentLink() will work
            ControllerContext.RouteData.DataTokens[RoutingConstants.NodeKey] = startPage.PageLink;

            Response.StatusCode = 404;
            ViewBag.Is404 = true;
            return View($"~/Views/Pages/{startPage.GetOriginalType().Name}.cshtml", startPage);
        }
    }

As you can see I'm using the view for the startpage to show the 404-message. The view checks if ViewBag.Is404 and displays an error message in a lightbox. There is no separate 404-page, the error message is edited on the start page.

Hope this helps! 

Note that this is a stripped down version of how I'm doing it, might have missed something when removing code

#173894
Jan 12, 2017 9:40
Vote:
 

I prefer doing a static 404 html page actually and use that if it doesn't have to be dynamic (depending on what you were trying to reach). Reason is basically that I've seen quite a few sites crash because you end up in an infinite loop of 404s in some cases. That combined with that 404 page is almost never changed => old school html page is not a bad idea actually. 

#173895
Jan 12, 2017 10:02
Vote:
 

Last time I checked the BVNetwork extension was a good and maintainable feature to use:

https://github.com/BVNetwork/404handler

#173900
Jan 12, 2017 11:12
Vote:
 

I have a site which is published in English and Danish. I have implemented the error page as follows.

Web.config:

<system.webServer> 
  <httpErrors errorMode="Custom" existingResponse="Auto">
      <remove statusCode="404" />
      <error statusCode="404" responseMode="ExecuteURL" prefixLanguageFilePath="en" path="/NotFound" />
  </httpErrors>

Global asax:

RouteTable.Routes.MapRoute(
                "NotFound",
                "NotFound",
                new {controller = "NotFoundPage", action = "Index"});

NotFoundPageController:

public ActionResult Index(PageData currentPage)
        {
            var notFoundPage = GlobalProperties.HomePageReference().Settings.NotFoundPage.GetContent<NotFoundPage>();

            // Set RoutingConstants.NodeKey so EPi's extension method RequestContext.GetContentLink() will work
            if (notFoundPage != null && ControllerContext?.RouteData != null)
            {
                ControllerContext.RouteData.DataTokens[RoutingConstants.NodeKey] = notFoundPage.PageLink;
            }

            if (Response != null) Response.StatusCode = 404;

            return View(new DefaultViewModel<NotFoundPage>(notFoundPage));
        }

My problem is that the 404-page is not presented in the correct language. I have checked that the error page is created and published in both languages inside Episerver but it always returns the Danish page (Danish is default language). This the case if I go to http://mysite/unknown-page or http://mysite/da/unknown-page or http://mysite/en/unknown-page.

Am I missing something in the config file that should pass the language to the NotFoundPageController or is it something else?

#186736
Edited, Jan 03, 2018 14:38
Vote:
 
Because this "redirect" to 404 page is done outside Episerver one solution that I've used before is something like this on the 404 page. To get culture from the requested page.
var urlRewriteContext = new UrlRewriteContext(new UrlBuilder(Request.RawUrl));
var currentCulture = urlRewriteContext.Language;
if (currentPage != null && currentCulture != null)
{
404PageType newpage;
repository.TryGet(currentPage.PageGuid, currentCulture, out newpage);

Then send newpage back instead of currentPage

#186975
Jan 09, 2018 16:13
Vote:
 

Thanks for the tip, Sebastian!

#187041
Jan 10, 2018 16:50
Vote:
 

The BVNetwork 404 Handler (https://nuget.episerver.com/en/OtherPages/Package/?packageId=BVN.404Handler) lets you redirect to any page in your CMS. You can then set up a page (call it "404" or something), and let the 404 handler send the visitor there when the page is not found in the page tree. As Daniel points out, this might throw you into an infinite loop, e.g. when what is missing is the page logo, and this logo is also used on your 404 page...

#187077
Jan 11, 2018 13:16
* 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.