Skip to main content

Error pages in DD4T (404 and 500)

In DD4T the 404 page can be retrieved as below, Override the Action Page, and when the Page is null return the NotFound Action. In the NotFound action, Query the Broker for the 404  page and return the view.

public class PageController : TridionPageController
{
public override ActionResult NotFound()
{
var page = ContentProvider.GetPageModel(WebRequestContext.Localization.Path + "/404.aspx");
if (page == null)
{
throw new HttpException(404, "Page Not Found");
}
Response.StatusCode = 404;
return View("Error", page);
}
public override ActionResult Page(string pageUrl)
{
var page = ContentProvider.GetPageModel(pageUrl);
if (page == null)
{
return NotFound();
}
//TODO:---
}
public ActionResult ServerError()
{
Response.StatusCode = 500;
return View("ServerError");
}
}

The 500 Error page can be throw from the Global level as below, when error occurred handle it the global level and return just view with error information

public class MvcApplication : NinjectHttpApplication
{
protected void Application_Error(object sender, EventArgs e)
{
if (Context.IsCustomErrorEnabled)
ShowCustomErrorPage(Server.GetLastError());
}
private void ShowCustomErrorPage(Exception exception)
{
HttpException httpException = exception as HttpException;
if (httpException == null)
httpException = new HttpException(500, "Internal Server Error", exception);
RouteData routeData = new RouteData();
Logger.Error("500 - Internal Server Error {0}", httpException);
routeData.Values.Add("controller", "Page");
routeData.Values.Add("action", "ServerError");
Server.ClearError();
IController controller = DependencyResolver.Current.GetService<PageController>(); ;
controller.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
}
}


Comments

Popular posts from this blog

Why not to have a static const in c#

This is just a thought, that I was thinking why can't we have a constant with static in C#, and the answer is 'NO'; That we cannot have a static constant; e.g: I created a class as below: public class Constants1 { public const string Const1 = "Hello"; public const string Const2 = "World"; public static string Static1 = "Hello Static"; } When we compile the program into IL, the C# compiler does a magic in IL, that the constants converts into static literals, of course it has to, that's why we are able to access the constants as Constants1.Const1

Beyond Solo Assistants: Google's Vision for AI Teamwork (Agent-to-Agent Collaboration)

We talk a lot about AI assistants like Google Assistant or chatbots answering our questions. They're pretty smart on their own, right? But imagine if they could team up, combine their unique skills, and tackle really complex problems together, just like a human team does. That's the core idea behind a super exciting area Google and others in the AI world are exploring: Agent-to-Agent (A2A) communication and collaboration. Think of it less as a single product called "Agent2Agent" and more as the science and engineering of building AI teams. Ready to explore why this is such a big deal? Let's break it down! First Off: What Even is an AI Agent? Think of an AI agent as a specialized digital helper. It's a piece of software designed to: Perceive: Understand its environment (text, images, data, user requests). Reason: Figure out the best course of action based on its goals and knowledge. Act: Perform tasks (answer questions, writ...

Create Document Dynamically (Umbraco)

You can create document and publish the content during run time, Below code snippets helps you to do it. public class UmbracoUtils { /// <summary> /// Create and publish the document programatically /// </summary> /// <param name="nodeName"></param> /// <param name="properties"></param> /// <param name="documentType"></param> /// <param name="parentId"></param> /// <returns>node id</returns> public static int CreateDocument(string nodeName, Dictionary<string, string> properties, int documentType, int parentId, List<string> roles = null) { DocumentType dt = new DocumentType(documentType); umbraco.BusinessLogic.User u = new umbraco.BusinessLogic.User(0); // Create the document Document d = Document.MakeNew(nodeName, dt, u, parentId); // Add values...