Request and response filters
A recent addition to ServiceStack is the ability to register custom Request and Response filters. These should be registered in your AppHost.Configure()
onload script:
The Request Filters are applied before the service gets called and accepts: (IRequest, IResponse, RequestDto) e.g:
//Add a request filter to check if the user has a session initialized
this.GlobalRequestFilters.Add((req, res, requestDto) => {
var sessionId = req.GetCookieValue("user-session");
if (sessionId == null)
{
res.ReturnAuthRequired();
}
});
The Response Filters are applied after your service is called and accepts: (IRequest, IResponse, ResponseDto) e.g:
//Add a response filter to add a 'Content-Disposition' header so browsers treat it as a native .csv file
this.GlobalResponseFilters.Add((req, res, responseDto) => {
if (req.ResponseContentType == ContentType.Csv)
{
res.AddHeader(HttpHeaders.ContentDisposition,
string.Format("attachment;filename={0}.csv", req.OperationName));
}
});
Tip: If you're writing your own response to the response stream inside the response filter, add res.EndRequest();
to signal to ServiceStack not to do anymore processing for this request.
A more typed API to register Global Request and Response filters per Request DTO Type are available under the RegisterTyped*
API's in AppHost where you can register both typed Request and Response Filters for HTTP and MQ Services independently:
void RegisterTypedRequestFilter<T>(Action<IRequest, IResponse, T> filterFn);
void RegisterTypedResponseFilter<T>(Action<IRequest, IResponse, T> filterFn);
void RegisterTypedMessageRequestFilter<T>(Action<IRequest, IResponse, T> filterFn);
void RegisterTypedMessageResponseFilter<T>(Action<IRequest, IResponse, T> filterFn);
Here's an example usage that enables more flexibility in multi-tenant solutions by attaching custom data on incoming requests, e.g:
public override void Configure(Container container)
{
RegisterTypedRequestFilter<Resource>((req, res, dto) =>
{
var route = req.GetRoute();
if (route != null && route.Path == "/tenant/{TenantName}/resource")
{
dto.SubResourceName = "CustomResource";
}
});
}
Typed Filters can also be used to apply custom behavior on Request DTO's sharing a common interface, e.g:
public override void Configure(Container container)
{
RegisterTypedRequestFilter<IHasSharedProperty>((req, res, dtoInterface) => {
dtoInterface.SharedProperty = "Is Shared";
});
}
Non-HTTP requests like Redis MQ are treated as Internal Requests which only execute the alternate GlobalMessageRequestFilters
and GlobalMessageResponseFilters
and Action Filter attributes.
- Why ServiceStack?
- Important role of DTOs
- What is a message based web service?
- Advantages of message based web services
- Why remote services should use separate DTOs
-
Getting Started
-
Designing APIs
-
Reference
-
Clients
-
Formats
-
View Engines 4. Razor & Markdown Razor
-
Hosts
-
Security
-
Advanced
- Configuration options
- Access HTTP specific features in services
- Logging
- Serialization/deserialization
- Request/response filters
- Filter attributes
- Concurrency Model
- Built-in profiling
- Form Hijacking Prevention
- Auto-Mapping
- HTTP Utils
- Dump Utils
- Virtual File System
- Config API
- Physical Project Structure
- Modularizing Services
- MVC Integration
- ServiceStack Integration
- Embedded Native Desktop Apps
- Auto Batched Requests
- Versioning
- Multitenancy
-
Caching
-
HTTP Caching 1. CacheResponse Attribute 2. Cache Aware Clients
-
Auto Query
-
AutoQuery Data 1. AutoQuery Memory 2. AutoQuery Service 3. AutoQuery DynamoDB
-
Server Events
-
Service Gateway
-
Encrypted Messaging
-
Plugins
-
Tests
-
ServiceStackVS
-
Other Languages
-
Amazon Web Services
-
Deployment
-
Install 3rd Party Products
-
Use Cases
-
Performance
-
Other Products
-
Future