-
Notifications
You must be signed in to change notification settings - Fork 84
Request and response filters
Demis Bellot edited this page Dec 3, 2013
·
1 revision
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: (IHttpRequest, IHttpResponse, RequestDto) e.g:
//Add a request filter to check if the user has a session initialized
this.RequestFilters.Add((httpReq, httpResp, requestDto) => {
var sessionId = httpReq.GetCookieValue("user-session");
if (sessionId == null)
{
httpResp.ReturnAuthRequired();
}
});
- The Response Filters are applied after your service is called and accepts: (IHttpRequest, IHttpResponse, ResponseDto) e.g:
//Add a response filter to add a 'Content-Disposition' header so browsers treat it as a native .csv file
this.ResponseFilters.Add((req, res, dto) => {
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.
- Why ServiceStack?
- What is a message based web service?
- Advantages of message based web services
- Why remote services should use separate DTOs
- Getting Started
- 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 caching options
- Built-in profiling
- Messaging and Redis
- Form Hijacking Prevention
- Auto-Mapping
- HTTP Utils
- Virtual File System
- Config API
- Physical Project Structure
- Modularizing Services
- MVC Integration
- Plugins 3. Request logger 4. Swagger API
- Tests
- Other Languages
- Use Cases
- Performance
- How To
- Future