1

See: Redirect to 404.html doesnt work on second time

We found that in case of a 404 error, DXA wants to open a custom page "error-404.html" in the Home Structure Group, like in the example website. However, we want to change the location of that page to a sub Structure Group folder (any maybe the name as well) but we cannot find where that setting is set. Can anyone tell us where that is set?

14-4-2021 Additional info after suggested solution of Atila Sos. This is the code that I created for the override of the NotFound method but somehow it is not hit when a non existing page is requested in the browser. What do I do wrong?

using System.Web; 
using System.Web.Mvc; 
using Sdl.Web.Common; 
using Sdl.Web.Common.Logging; 
using Sdl.Web.Common.Models; 
using Sdl.Web.Mvc.Configuration; 
using Sdl.Web.Mvc.Formats; 
namespace Sdl.Web.Mvc.Controllers 
{
public class CustomPageController : PageController 
{ 
/// <summary> 
/// Render a custom file not found page 
/// </summary> 
/// <returns>404 page or HttpException if there is none</returns> 
[FormatData] 
public override ActionResult NotFound() 
{ 
using (new Tracer()) 
{     
string notFoundPageUrl = WebRequestContext.Localization.Path + "/Subfolder/Errors" + "/404"; 
PageModel pageModel; 
try 
{ 
pageModel = ContentProvider.GetPageModel(notFoundPageUrl, WebRequestContext.Localization); 
} 
catch (DxaItemNotFoundException ex) 
{ 
Log.Error(ex); 
throw new HttpException(404, ex.Message); 
} 
SetupViewData(pageModel); 
PageModel model = EnrichModel(pageModel) as PageModel ?? pageModel; 
WebRequestContext.PageModel = model; 
Response.StatusCode = 404; 
return View(model.MvcData.ViewName, model); 
} 
} 
} 
} 
0
4

The location is hardcoded within DXA's source code. Take a look at the PageController's NotFound method.

string notFoundPageUrl = WebRequestContext.Localization.Path + "/error-404";

The method is virtual so you can override it. Truth be told, it would be better to have direct control over just the location :)

3
  • I followed your advise to override the NotFound methode (see the comment and code that I added to the original request). Can you tell me what I do wrong in the code? – Raymond Hempenius Apr 14 at 15:11
  • 1
    Have you "relinked" your route to your CustomPageController? – Atila Sos Apr 14 at 15:24
  • Thanks Atila Sos, your answer helped me to solve this. So the complete solution was this. Besides creating the custom pagecontroller as mentioned in the original request, I had to change the routing of all the methods of the page controller to custom page controller in the global.asax. So, in the routing, everywhere where controller= "Page" is set, it should be changed to controller= "CustomPage". Finally, in the solution, I had to rename the views folder from "Page" to "CustomPage". – Raymond Hempenius Apr 19 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.