Skip to content

Instantly share code, notes, and snippets.

@thecodejunkie
Created March 26, 2012 16:38
Show Gist options
  • Save thecodejunkie/2206393 to your computer and use it in GitHub Desktop.
Save thecodejunkie/2206393 to your computer and use it in GitHub Desktop.
Rendering a razor view from a custom error handler
public class CustomErrorHandler : IErrorHandler
{
private readonly IViewFactory factory;
private readonly IViewLocationCache cache;
public CustomErrorHandler(IViewFactory factory, IViewLocationCache cache)
{
this.factory = factory;
this.cache = cache;
}
public bool HandlesStatusCode(HttpStatusCode statusCode)
{
return true; // statusCode == HttpStatusCode.NotFound;
}
public void Handle(HttpStatusCode statusCode, NancyContext context)
{
var foundMatchingView = this.cache.Any(x =>
x.Name.Equals(Convert.ToString((int) statusCode)) &&
x.Extension.Equals("cshtml", StringComparison.OrdinalIgnoreCase));
if (foundMatchingView)
{
var viewContext =
new ViewLocationContext { Context = context };
var viewName =
string.Concat(Convert.ToString((int)statusCode), ".cshtml");
context.Response = this.factory.RenderView(viewName, null, viewContext);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment