Git Product home page Git Product logo

hydrogen.extensions.mvc5's Introduction

Hydrogen Extensions for ASP.NET MVC 5

Hydrogen.Extensions.Mvc5.Async

Get it on NuGet.

Adds support for async filters (i.e. action filters, etc.) by implementing a custom IAsyncActionInvoker.

Read the blog article about the alpha release.

How to use:

Swap out the default implementation IAsyncActionInvoker with Hydrogen.Extensions.Mvc5.Async.ControllerActionInvokerEx. There are two ways to do this:

  1. Set the ActionInvoker property of a controller when it's constructed.

    public class HomeController : Controller { public HomeController() { ActionInvoker = ControllerActionInvokerEx.Instance; } }

  2. Using your DI container of choice, register Hydrogen.Extensions.Mvc5.Async.ControllerActionInvokerEx as an implementation of IAsyncActionInvoker. You may register ControllerActionInvokerEx.Instance as a singleton.

For example (using Autofac):

builder.RegisterInstance(ControllerActionInvokerEx.Instance)
        .As<IAsyncActionInvoker>()
        .SingleInstance();

Creating an async filter

You may create a subclass of AsyncActionFilterAttribute or AsyncExceptionFilterAttribute and override the async methods.

For example:

public class MyAsyncActionFilterAttribute : AsyncActionFilterAttribute
{
    public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
    {
        // Execute code before the action is invoked.
        // You can "short-circuit" the action by setting 'context.Result'
        // and returning before calling 'next()'.
        
        // The implementation is responsible for calling 'next()'.
        var actionExecutedContext = await next().ConfigureAwait(false);

        // Execute code after the action is invoked
    }

    public override async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
    {
        // Execute code before the result is invoked.
        // You can "short-circuit" the action by setting 'context.Canceled = true'
        // and returning before calling 'next()'.
        
        // The implementation is responsible for calling 'next()'.
        var resultExecutedContext = await next().ConfigureAwait(false);

        // Execute code after the result is invoked
    }
}

Alternately you may implement any of the async filters:

  • IAsyncActionFilter
  • IAsyncResultFilter
  • IAsyncExceptionFilter
  • IAsyncAuthorizationFilter

Unfortunately, for compatibility reasons, you must also implement the non-async version of these filters (e.g. void OnActionExecuting(ActionExecutingContext filterContext)). However, these methods can be a NOOP since the code is never executed by ControllerActionInvokerEx.

This library, currently, does not implement as async version of IAuthenticationFilter.

hydrogen.extensions.mvc5's People

Contributors

jdaigle avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.