diff --git a/src/NotFoundMvc/ActionInvokerSelector.cs b/src/NotFoundMvc/ActionInvokerSelector.cs new file mode 100644 index 0000000..fb00306 --- /dev/null +++ b/src/NotFoundMvc/ActionInvokerSelector.cs @@ -0,0 +1,16 @@ +namespace NotFoundMvc +{ + using System; + using System.Web.Mvc; + + internal static class ActionInvokerSelector + { + private static readonly Func Mvc3Invoker = + originalActionInvoker => new ActionInvokerWrapper(originalActionInvoker); + + private readonly static Func Mvc4Invoker = + originalActionInvoker => new NotFoundAsyncControllerActionInvoker(); + + public static Func Current { get; } = typeof(Controller).Assembly.GetName().Version.Major <= 3 ? Mvc3Invoker : Mvc4Invoker; + } +} \ No newline at end of file diff --git a/src/NotFoundMvc/ControllerFactoryWrapper.cs b/src/NotFoundMvc/ControllerFactoryWrapper.cs index 5e9069d..325e43e 100644 --- a/src/NotFoundMvc/ControllerFactoryWrapper.cs +++ b/src/NotFoundMvc/ControllerFactoryWrapper.cs @@ -49,7 +49,7 @@ private static void WrapControllerActionInvoker(IController controller) var controllerWithInvoker = controller as Controller; if (controllerWithInvoker != null) { - controllerWithInvoker.ActionInvoker = new ActionInvokerWrapper(controllerWithInvoker.ActionInvoker); + controllerWithInvoker.ActionInvoker = ActionInvokerSelector.Current(controllerWithInvoker.ActionInvoker); } } } diff --git a/src/NotFoundMvc/NotFoundAsyncControllerActionInvoker.cs b/src/NotFoundMvc/NotFoundAsyncControllerActionInvoker.cs new file mode 100644 index 0000000..441c2d8 --- /dev/null +++ b/src/NotFoundMvc/NotFoundAsyncControllerActionInvoker.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Web.Mvc; +using System.Web.Mvc.Async; + +namespace NotFoundMvc +{ + public class NotFoundAsyncControllerActionInvoker : AsyncControllerActionInvoker + { + protected override ActionDescriptor FindAction(ControllerContext controllerContext, ControllerDescriptor controllerDescriptor, + string actionName) + { + var result = base.FindAction(controllerContext, controllerDescriptor, actionName); + if (result == null) + { + return new NotFoundActionDescriptor(); + } + return result; + } + } + + public class NotFoundActionDescriptor : ActionDescriptor + { + public override object Execute(ControllerContext controllerContext, IDictionary parameters) + { + if (controllerContext == null) + { + throw new ArgumentNullException(nameof(controllerContext)); + } + + INotFoundController notFound = NotFoundHandler.CreateNotFoundController(controllerContext.RequestContext); + controllerContext.RouteData.Values["action"] = "NotFound"; + return notFound.NotFound(); + } + + public override ParameterDescriptor[] GetParameters() + { + return new ParameterDescriptor[] { }; + } + + public override string ActionName + { + get { return "NotFound"; } + } + + public override ControllerDescriptor ControllerDescriptor + { + get { return new ReflectedControllerDescriptor(typeof(NotFoundController)); } + } + } +} \ No newline at end of file diff --git a/src/NotFoundMvc/NotFoundController.cs b/src/NotFoundMvc/NotFoundController.cs index c785fe4..e18c2b2 100644 --- a/src/NotFoundMvc/NotFoundController.cs +++ b/src/NotFoundMvc/NotFoundController.cs @@ -1,9 +1,8 @@ namespace NotFoundMvc { using System.Web.Mvc; - using System.Web.Routing; - public class NotFoundController : ControllerBase + public class NotFoundController : ControllerBase, INotFoundController { public ActionResult NotFound() { @@ -15,4 +14,9 @@ protected override void ExecuteCore() new NotFoundViewResult().ExecuteResult(this.ControllerContext); } } + + public interface INotFoundController : IController + { + ActionResult NotFound(); + } } \ No newline at end of file diff --git a/src/NotFoundMvc/NotFoundHandler.cs b/src/NotFoundMvc/NotFoundHandler.cs index 39d56eb..404d67b 100644 --- a/src/NotFoundMvc/NotFoundHandler.cs +++ b/src/NotFoundMvc/NotFoundHandler.cs @@ -2,14 +2,14 @@ { using System; using System.Web; - using System.Web.Mvc; using System.Web.Routing; + using NotFoundMvc; public class NotFoundHandler : IHttpHandler { - private static Func createNotFoundController = context => new NotFoundController(); + private static Func createNotFoundController = context => new NotFoundController(); - public static Func CreateNotFoundController + public static Func CreateNotFoundController { get { diff --git a/src/NotFoundMvc/NotFoundMvc.csproj b/src/NotFoundMvc/NotFoundMvc.csproj index d768834..025ffa0 100644 --- a/src/NotFoundMvc/NotFoundMvc.csproj +++ b/src/NotFoundMvc/NotFoundMvc.csproj @@ -74,11 +74,13 @@ + +