Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Epic.Infrastructure {
- using System;
- using System.Linq;
- using Nancy;
- using Nancy.ErrorHandling;
- using Nancy.ViewEngines;
- public class EpicErrorHandler : CustomRazorErrorHandler {
- public EpicErrorHandler(IViewFactory factory, IViewLocationCache cache) : base(factory, cache) {}
- public override bool HandlesStatusCode(HttpStatusCode statusCode, NancyContext context) {
- return statusCode == HttpStatusCode.NotFound || statusCode == HttpStatusCode.InternalServerError;
- }
- public override void Handle(HttpStatusCode statusCode, NancyContext context) {
- switch (statusCode) {
- case HttpStatusCode.NotFound:
- RenderView(context, "404");
- break;
- case HttpStatusCode.InternalServerError:
- RenderView(context, "500");
- break;
- }
- context.Response.StatusCode = statusCode;
- }
- }
- public abstract class CustomRazorErrorHandler : IErrorHandler {
- private readonly IViewLocationCache _cache;
- private readonly IViewFactory _factory;
- protected CustomRazorErrorHandler(IViewFactory factory, IViewLocationCache cache) {
- _factory = factory;
- _cache = cache;
- }
- #region IErrorHandler Members
- public abstract bool HandlesStatusCode(HttpStatusCode statusCode, NancyContext context);
- public abstract void Handle(HttpStatusCode statusCode, NancyContext context);
- #endregion
- protected void RenderView(NancyContext context, string viewName, dynamic model = null) {
- bool foundMatchingView = _cache.Any(x =>
- x.Name.Equals(viewName) &&
- x.Extension.Equals("cshtml", StringComparison.OrdinalIgnoreCase));
- if (foundMatchingView) {
- var viewContext = new ViewLocationContext {Context = context};
- context.Response = _factory.RenderView(viewName, model, viewContext);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment