Controller base class now implements IActionFilter
This commit is contained in:
parent
ae4e3bc61d
commit
28fee3470f
|
|
@ -78,5 +78,10 @@ namespace MvcSample.Web
|
||||||
{
|
{
|
||||||
throw new Exception(message);
|
throw new Exception(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override void OnActionExecuting(ActionExecutingContext context)
|
||||||
|
{
|
||||||
|
ViewBag.DidTheFilterRun = "Totally!";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -15,13 +15,45 @@
|
||||||
// See the Apache 2 License for the specific language governing
|
// See the Apache 2 License for the specific language governing
|
||||||
// permissions and limitations under the License.
|
// permissions and limitations under the License.
|
||||||
|
|
||||||
|
using System.Diagnostics;
|
||||||
|
using System.Globalization;
|
||||||
|
using Microsoft.AspNet.Mvc;
|
||||||
|
|
||||||
namespace MvcSample.Web
|
namespace MvcSample.Web
|
||||||
{
|
{
|
||||||
public class SimplePocoController
|
public class SimplePocoController : IActionFilter, IResultFilter
|
||||||
{
|
{
|
||||||
|
private Stopwatch _timer;
|
||||||
|
|
||||||
public string Index()
|
public string Index()
|
||||||
{
|
{
|
||||||
return "Hello world";
|
return "Hello world";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void OnActionExecuting(ActionExecutingContext context)
|
||||||
|
{
|
||||||
|
_timer = Stopwatch.StartNew();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnActionExecuted(ActionExecutedContext context)
|
||||||
|
{
|
||||||
|
var time = _timer.ElapsedMilliseconds;
|
||||||
|
context.HttpContext.Response.Headers.Add(
|
||||||
|
"ActionElapsedTime",
|
||||||
|
new string[] { time.ToString(CultureInfo.InvariantCulture) + " ms" });
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnResultExecuting(ResultExecutingContext context)
|
||||||
|
{
|
||||||
|
_timer = Stopwatch.StartNew();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void OnResultExecuted(ResultExecutedContext context)
|
||||||
|
{
|
||||||
|
var time = _timer.ElapsedMilliseconds;
|
||||||
|
context.HttpContext.Response.Headers.Add(
|
||||||
|
"ResultElapsedTime",
|
||||||
|
new string[] { time.ToString(CultureInfo.InvariantCulture) + " ms" });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Security.Principal;
|
using System.Security.Principal;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNet.Abstractions;
|
using Microsoft.AspNet.Abstractions;
|
||||||
using Microsoft.AspNet.Mvc.Core;
|
using Microsoft.AspNet.Mvc.Core;
|
||||||
using Microsoft.AspNet.Mvc.ModelBinding;
|
using Microsoft.AspNet.Mvc.ModelBinding;
|
||||||
|
|
@ -25,7 +26,7 @@ using Microsoft.AspNet.Mvc.Rendering;
|
||||||
|
|
||||||
namespace Microsoft.AspNet.Mvc
|
namespace Microsoft.AspNet.Mvc
|
||||||
{
|
{
|
||||||
public class Controller
|
public class Controller : IActionFilter, IAsyncActionFilter
|
||||||
{
|
{
|
||||||
private DynamicViewData _viewBag;
|
private DynamicViewData _viewBag;
|
||||||
|
|
||||||
|
|
@ -223,5 +224,24 @@ namespace Microsoft.AspNet.Mvc
|
||||||
{
|
{
|
||||||
return new RedirectToRouteResult(Url, routeName, routeValues, permanent: true);
|
return new RedirectToRouteResult(Url, routeName, routeValues, permanent: true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public virtual void OnActionExecuting([NotNull] ActionExecutingContext context)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual void OnActionExecuted([NotNull] ActionExecutedContext context)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public virtual async Task OnActionExecutionAsync(
|
||||||
|
[NotNull] ActionExecutingContext context,
|
||||||
|
[NotNull] ActionExecutionDelegate next)
|
||||||
|
{
|
||||||
|
OnActionExecuting(context);
|
||||||
|
if (context.Result == null)
|
||||||
|
{
|
||||||
|
OnActionExecuted(await next());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -88,14 +88,6 @@ namespace Microsoft.AspNet.Mvc.Filters
|
||||||
|
|
||||||
ApplyFilterToContainer(filterItem.Filter, filterFactory);
|
ApplyFilterToContainer(filterItem.Filter, filterFactory);
|
||||||
}
|
}
|
||||||
|
|
||||||
var controllerFilter = context.ActionContext.Controller as IFilter;
|
|
||||||
if (controllerFilter != null)
|
|
||||||
{
|
|
||||||
// If the controller implements a filter, we want it to be the first to run.
|
|
||||||
var descriptor = new FilterDescriptor(controllerFilter, FilterScope.Action);
|
|
||||||
context.Result.Insert(0, new FilterItem(descriptor, controllerFilter));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void InsertControllerAsFilter(FilterProviderContext context, IFilter controllerFilter)
|
private void InsertControllerAsFilter(FilterProviderContext context, IFilter controllerFilter)
|
||||||
|
|
@ -104,8 +96,9 @@ namespace Microsoft.AspNet.Mvc.Filters
|
||||||
// run closest to the action.
|
// run closest to the action.
|
||||||
int order = Int32.MaxValue;
|
int order = Int32.MaxValue;
|
||||||
var orderedControllerFilter = controllerFilter as IOrderedFilter;
|
var orderedControllerFilter = controllerFilter as IOrderedFilter;
|
||||||
if (orderedControllerFilter == null)
|
if (orderedControllerFilter != null)
|
||||||
{
|
{
|
||||||
|
|
||||||
order = orderedControllerFilter.Order;
|
order = orderedControllerFilter.Order;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -110,7 +110,6 @@
|
||||||
<Compile Include="Filters\ResultExecutionDelegate.cs" />
|
<Compile Include="Filters\ResultExecutionDelegate.cs" />
|
||||||
<Compile Include="Filters\ResultFilterAttribute.cs" />
|
<Compile Include="Filters\ResultFilterAttribute.cs" />
|
||||||
<Compile Include="Filters\ServiceFilterAttribute.cs" />
|
<Compile Include="Filters\ServiceFilterAttribute.cs" />
|
||||||
<Compile Include="ControllerFilterProvider.cs" />
|
|
||||||
<Compile Include="FormContext.cs" />
|
<Compile Include="FormContext.cs" />
|
||||||
<Compile Include="HttpDeleteAttribute.cs" />
|
<Compile Include="HttpDeleteAttribute.cs" />
|
||||||
<Compile Include="HttpGetAttribute.cs" />
|
<Compile Include="HttpGetAttribute.cs" />
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue