Skip to content

Commit

Permalink
Merge pull request #44 from mortan/pr-async-actions
Browse files Browse the repository at this point in the history
Fix issue 20 (async actions)
  • Loading branch information
DavidDeSloovere committed Sep 21, 2016
2 parents 41172aa + 0f50199 commit 7962f68
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 6 deletions.
16 changes: 16 additions & 0 deletions src/NotFoundMvc/ActionInvokerSelector.cs
@@ -0,0 +1,16 @@
namespace NotFoundMvc
{
using System;
using System.Web.Mvc;

internal static class ActionInvokerSelector
{
private static readonly Func<IActionInvoker, IActionInvoker> Mvc3Invoker =
originalActionInvoker => new ActionInvokerWrapper(originalActionInvoker);

private readonly static Func<IActionInvoker, IActionInvoker> Mvc4Invoker =
originalActionInvoker => new NotFoundAsyncControllerActionInvoker();

public static Func<IActionInvoker, IActionInvoker> Current { get; } = typeof(Controller).Assembly.GetName().Version.Major <= 3 ? Mvc3Invoker : Mvc4Invoker;
}
}
2 changes: 1 addition & 1 deletion src/NotFoundMvc/ControllerFactoryWrapper.cs
Expand Up @@ -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);
}
}
}
Expand Down
51 changes: 51 additions & 0 deletions 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<string, object> 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)); }
}
}
}
8 changes: 6 additions & 2 deletions 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()
{
Expand All @@ -15,4 +14,9 @@ protected override void ExecuteCore()
new NotFoundViewResult().ExecuteResult(this.ControllerContext);
}
}

public interface INotFoundController : IController
{
ActionResult NotFound();
}
}
6 changes: 3 additions & 3 deletions src/NotFoundMvc/NotFoundHandler.cs
Expand Up @@ -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<RequestContext, IController> createNotFoundController = context => new NotFoundController();
private static Func<RequestContext, INotFoundController> createNotFoundController = context => new NotFoundController();

public static Func<RequestContext, IController> CreateNotFoundController
public static Func<RequestContext, INotFoundController> CreateNotFoundController
{
get
{
Expand Down
2 changes: 2 additions & 0 deletions src/NotFoundMvc/NotFoundMvc.csproj
Expand Up @@ -74,11 +74,13 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="ActionInvokerSelector.cs" />
<Compile Include="ActionInvokerWrapper.cs" />
<Compile Include="NotFoundConfig.cs" />
<Compile Include="ControllerFactoryWrapper.cs" />
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="InstallerModule.cs" />
<Compile Include="NotFoundAsyncControllerActionInvoker.cs" />
<Compile Include="NotFoundController.cs" />
<Compile Include="NotFoundHandler.cs" />
<Compile Include="NotFoundViewResult.cs" />
Expand Down

0 comments on commit 7962f68

Please sign in to comment.