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

Localization in Umbraco, Item Page field localization

Umbraco is one of the most deployed Web Content Management Systems on the Microsoft stack. It's in the top five most popular server applications and among the ten most popular open source tools in general. You can directly download and start using it from http://www.umbraco.com If you have not used the application yet, its always worth trying it once. I am sure you will love it. Well, for the first time users, Umbraco provide step by step instruction for initial set up, once you are done you can use some sample template or create a blank website. To know more on umbraco, Please refer below links http://our.umbraco.org/wiki/how-tos/a-complete-newbie's-guide-to-umbraco http://umbraco.com/help-and-support/video-tutorials/getting-started?freeVideos=1 http://our.umbraco.org/wiki/how-tos/getting-started-with-umbraco-what-is-next-after-you-install http://our.umbraco.org/wiki/install-and-setup ,  I have created a blank website, Now we will go step by step. Fi...

pjax with MVC, and form post like pjax

Last weekend I was working on a interesting project, and came across an interesting JavaScript library called pjax. More information on pjax: https://github.com/defunkt/jquery-pjax/ http://pjax.heroku.com/ pjax loads HTML from your server into the current page without a full reload. It's ajax with real permalinks, page titles, and a working back button that fully degrades, It just enhances the browsing experience. I just loved the way we could use it with MVC; Below code sample explains how we could use the same with MVC. Clientside code would look as below: Since the Form post is not handled with pjax, we can do a work around as below. Updated _ViewStart.cshtml as below: @{ if (Request.Headers["X-PJAX"] != null || Request.Headers["AJAX-FORM"]!=null) { Layout = "~/Views/Shared/_PjaxLayout.cshtml"; } else { Layout = "~/Views/Shared/_Layout.cshtml"; } } _PjaxLayout.cshtml ...

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