I need to generate PDF reports of certain data sets when the client passes a certain parameter (?print
) in the query string of a Web API route. Now I'm wondering if a custom action filter is a suitable way of doing this.
public class ReportFilterAttribute : ActionFilterAttribute {
public ReportFilterAttribute(string controller, string layoutPath) {
}
public override void OnActionExecuting(HttpActionContext actionContext)
{
// look for parameter ?print in the request query string
}
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
// load layout file
// fill in data returned by the api
// manipulate response to return a filestream instead of json data
}
}
- Is there a way to prevent
OnActionExecuted
being called, e.g. when there is no parameter?print
available? - Is it an acceptable practice to return either a filestream (PDF) or JSON data, depending on the request (how should a client know about this?)
- Is it fine to do this using an action filter or should I better write e.g. a custom OWIN middleware?
print
parameter in the action filter, I don't think you can do that in the WebApiConfig.cs (ie: register the action filter only whenprint
param is there). As for the filestream, I'd say as long as you are returning the correct content headers, the client should know fine and react accordingly - stackoverflow.com/questions/9541351/…