Skip to content

Custom Error Pages

Joel Mitchell edited this page Mar 23, 2017 · 5 revisions

By default the stock Cofoundry pages will use your default master page to render in, but usually you'll have your own custom styles and you'll want to do more customization.

Cofoundry lets you customize a handful of different error pages. Each one should be placed in your 'Views/Shared' folder, where it will be picked up by Cofoundry.

Not Found (404)

This file should be named NotFound.cshtml and can use the NotFoundPageViewModel as the model.

@inherits CofoundryPage<NotFoundPageViewModel>

<h1>Not Found</h1>

<p>
    Sorry, that page could not be found
</p>

In your controllers you can also take advantage of the INotFoundViewHelper to return a 404 result. This has the added benefit of checking for Rewrite Rules and automatically redirecting:

using Cofoundry.Web;

public class ProductController : Controller
{
    private readonly INotFoundViewHelper _notFoundViewHelper;
    private readonly IProductRepository _productRepository;

    public ProductController(
        INotFoundViewHelper notFoundViewHelper,
        IProductRepository productRepository
        )
    {
        _notFoundViewHelper = notFoundViewHelper;
        _productRepository = productRepository;
    }

    public ActionResult Details(int id)
    {
        var product = _productRepository.GetById(id);

        if (product == null)
        {
            return _notFoundViewHelper.GetView();
        }

        return View(product);
    }
}

Error (500)

This file should be named Error.cshtml and can use the ErrorPageViewModel as the model.

@inherits CofoundryPage<ErrorPageViewModel>

<h1>Error!</h1>

<p>
    There has been an error! Sorry about that...
</p>

Forbidden (403)

This file should be named Forbidden.cshtml and does not have a view model.

@inherits CofoundryPage

<h1>Access denied</h1>

<p>
    You don't have permission to view this page
</p>

Clone this wiki locally