【发布时间】:2015-04-07 10:46:40
【问题描述】:
当客户端在 Web API 路由的查询字符串中传递某个参数 (?print) 时,我需要生成某些数据集的 PDF 报告。现在我想知道自定义操作过滤器是否适合这样做。
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
}
}
- 有没有办法防止
OnActionExecuted被调用,例如当没有可用的参数?print时? - 是否可以根据请求返回文件流 (PDF) 或 JSON 数据(客户应该如何知道这一点?)
- 是否可以使用操作过滤器执行此操作,或者我应该更好地编写例如自定义 OWIN 中间件?
【问题讨论】:
-
除了在动作过滤器中检查
print参数是否存在,我认为你不能在WebApiConfig.cs中这样做(即:仅在@987654327时注册动作过滤器@ 参数在那里)。至于文件流,我想说,只要您返回正确的内容标头,客户端就应该知道并做出相应的反应 - stackoverflow.com/questions/9541351/…
标签: c# asp.net asp.net-web-api owin