19 February, 2015

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));
}
}


Related Posts:

  • Navigation in DD4T There always been a question on What is the best approach for building navigation with DD4T and ASP.NET MVC? Answer is: There is TridionSiteMapProvi… Read More
  • Dynamic Publication Resolving in DD4T DD4T is a great framework which would give you everything in place what you basically needed. Here we will see how we could resolve the publicatio… Read More
  • 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 actio… Read More
  • DD4T: Customizing RenderComponentPresentation Sometime when we work with Component presentation, we might come up with the scenario to have customized RenderComponentPresentation, when we deal wi… Read More
  • What is DD4T Well, It's the high time to think about DD4T. What is DD4T? What makes more easier to the developer and Editor to talk about DD4T.!!  DD4T is a … Read More

0 comments:

Post a Comment