Show / Hide Table of Contents

Actions

You can add Actions to a route that run before and after the Operation. Actions are provided the current IApiContext<TModel, TUser> and dataset and don't return anything. Pre-operation Actions are the seventh step of the request flow, after condition checks, and are typically used to modify the parsed request body before it's sent to the Operation. Post-operation Actions are the ninth step of the request flow, before result writing, and are typically used to modify the API response before it's sent.

Builder Methods

Low-Level Methods

These methods are primarily used when you've implemented your own IPostOpAction<TModel, TUser> or IPreOpAction<TModel, TUser> and want to attach it to a route.

  • AddPostOpAction(IPostOpAction<TModel, TUser>)
  • AddPostOpAction<TAction>()
  • AddPreOpAction(IPreOpAction<TModel, TUser>)
  • AddPreOpAction<TAction>()
  • ClearPostOpActions()
  • ClearPreOpActions()

High-Level Methods

Builder Prefix: Before, After

  • After(Action<IApiContext<TModel, TUser>, IEnumerable<TModel>>)
  • AfterAsync(Func<IApiContext<TModel, TUser>, IEnumerable<TModel>, Task>)
  • Before(Action<IApiContext<TModel, TUser>, IQueryable<TModel>>)
  • BeforeAsync(Func<IApiContext<TModel, TUser>, IQueryable<TModel>, Task>)
  • Deprecate(String)
  • ForEach(Action<TModel>)
  • ForEachAsync(Func<TModel, Task>)
  • ForEachInput(Action<TModel>)
  • ForEachInputAsync(Func<TModel, Task>)
  • SetHeader(String, Func<IApiContext<TModel, TUser>, IEnumerable<TModel>, String>)
  • SetHeader(String, String)
  • SetHeaderAsync(String, Func<IApiContext<TModel, TUser>, IEnumerable<TModel>, Task<String>>)
  • SetValue(Expression<Func<TModel, Object>>, ParameterRetriever)
  • SetValue(Expression<Func<TModel, Object>>, Func<IApiContext<TModel, TUser>, Object>)
  • SetValue(PropertyInfo, ParameterRetriever)
  • SetValue(PropertyInfo, Func<IApiContext<TModel, TUser>, Object>)
  • SetValueAsync(Expression<Func<TModel, Object>>, Func<IApiContext<TModel, TUser>, Task<Object>>)
  • SetValueAsync(PropertyInfo, Func<IApiContext<TModel, TUser>, Task<Object>>)
  • SetValueQuery(Expression<Func<TModel, Object>>)
  • SetValueQuery(Expression<Func<TModel, Object>>, String)
  • SetValueQuery(PropertyInfo)
  • SetValueQuery(PropertyInfo, String)
  • SetValueRoute(Expression<Func<TModel, Object>>)
  • SetValueRoute(Expression<Func<TModel, Object>>, String)
  • SetValueRoute(PropertyInfo)
  • SetValueRoute(PropertyInfo, String)
  • WriteResponseCountValue()
  • WriteResponseValue(String, Func<IApiContext<TModel, TUser>, IEnumerable<TModel>, Object>)
  • WriteResponseValue(String, Object)
  • WriteResponseValue<TAttribute>(Func<IApiContext<TModel, TUser>, IEnumerable<TModel>, Object>)
  • WriteResponseValue<TAttribute>(Object)
  • WriteResponseValueAsync(String, Func<IApiContext<TModel, TUser>, IEnumerable<TModel>, Task<Object>>)
  • WriteResponseValueAsync<TAttribute>(Func<IApiContext<TModel, TUser>, IEnumerable<TModel>, Task<Object>>)
  • WriteVersion(String)
  • WriteVersionHeader(String)
  • WriteVersionValue(String)

Examples

Setting a value on a parsed model before the operation

The SetValue methods can be used to set properties' values on parsed models before the operation.

This example will always set the Value of the model to 4

C#
app.UseSeltzr<MyModel>(api => {
    api.SetValue(m => m.Value, (c) => 4);
});

The SetValue(Expression<Func<TModel, Object>>, Func<IApiContext<TModel, TUser>, Object>) method uses a Func parameter to get the desired value of the property, which is passed the current API context.

This example uses a service to get the desired value for the Value property.

C#
app.UseSeltzr<MyModel>(api => {
    api.SetValue(m => m.Value, (c) => c.Services.GetRequiredService<IValueManager>().GetNextValue());
});

The SetValueQuery and SetValueRoute methods can also be used to set properties using request parameters.

C#
app.UseSeltzr<MyModel>("{val}", api => {
    api.SetValueQuery(m => m.ChildId); // query parameter name is inferred to be "childId"
    api.SetValueRoute(m => m.Value, "val");
});

Modifying values of parsed models before the operation

The ForEachInput(Action<TModel>) and ForEachInputAsync(Func<TModel, Task>) methods can be used to modify existing values on parsed models before the operation.

This example trims the Name field of a model before it's created or updated.

C#
app.UseSeltzr<MyModel>(api => {
    api.ForEachInput(m => m.Name = m.Name.Trim());
});

Logging events with Actions

The After and Before methods can be used for logging events.

This example injects an ILogger<TCategoryName> to log after the operation.

C#
app.UseSeltzr<MyModel>(api => {
    api.After((c, d) => {
		c.Services.GetRequiredService<ILogger<CreateOperation>>.LogInformation("Created {count} models", d.Count());
	});
});

Setting response values

See Response Wrapping for details on using the WriteValue methods.

  • Improve this Doc
Back to top Generated by DocFX