GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Request and response filters Request and response filters
Demis Bellot edited this page
This page has moved to docs.servicestack.netA recent addition to ServiceStack is the ability to register custom Request and Response filters. These should be registered in your 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 A more typed API to register Global Request and Response filters per Request DTO Type are available under the 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
Clone this wiki locally |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |