OWIN middleware that allows sending actions to a single page application in the form of a cookie or redirect.
When the middleware is challenged, it will redirect to a fixed URL passing an action object in the URL or return an JavaScript readable cookie containing an action object as JSON.
This is useful in cases where the back-end requests a single-page application to perform a certain action without having to hardcode different front-end routes.
F.e. when sending out notification e-mails, you sometimes want to include back-end links in the e-mail.
Get it on NuGet
PM> Install-Package OwinActionMiddleware
PM> Install-Package OwinActionMiddleware.WebApi
The base package contains an extension method to be used on the IOwinContext
interface, while the WebApi package adds an extension method to be used in an ApiController
.
app.Use(async (ctx, next) =>
{
if (ctx.Request.Path != new PathString("/test")) await next();
ctx.ChallengeActionMiddleware(new ActionData { Action = "JumpAround" });
ctx.Response.StatusCode = (int)HttpStatusCode.OK;
});
[RoutePrefix("navigate")]
public class MyApiController : ApiController
{
[HttpGet, Route("userprofile")]
public IHttpActionResult Action(Guid userId)
{
return this.Action(new OpenUserProfileAction(userId));
}
}
public class OpenUserProfileAction : Action
{
public OpenUserProfileAction(Guid userId)
{
Action = "OpenUserProfile";
UserId = userId;
}
public Guid UserId { get; }
}