本文介绍了使用Play Framework资产控制器返回资产时需要添加标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要返回带有标题的pdf文件

I need to return pdf files with the header

X-Robots-Tag: noindex

https: //developers.google.com/webmasters/control-crawl-index/docs/robots_meta_tag?hl=de#robots-meta-tag-verwenden

是否可以使用标准播放的Assets Controller执行此操作?看一下源代码,似乎唯一可配置的是:

Is there a way to do this using the standard play Assets Controller? Took a look inside the source code and the only thing that seems to be configurable is:

val configuredCacheControl = config(_.getString("\"assets.cache." + name + "\""))

谢谢

推荐答案

我可能会接受 Kris 的想法.像这样的事情应该可以带您去那里,基本上我要做的就是利用Play!提供静态资源但在结果中添加标头的控制器:

I might go with Kris's idea. Something kinda like this should get you part of the way there, basically all I'm doing is leveraging the Play! Controller that serves static resources but adding a header to the result:

object AssetsController extends Controller {

def modifiedAsset(path: String, file: String): Future[Result] = Async.action { implicit request =>
  Assets.at(path, file)(request).map { result =>
   result.withHeaders(("X-Robots-Tag", "noindex"))
  }
}

然后将您的routes文件需要从controllers.Assets.at(path="/public", file)修改为controllers.AssetsController.modifiedAsset(path="/public", file)

Then your routes file would need to be modified from controllers.Assets.at(path="/public", file) to controllers.AssetsController.modifiedAsset(path="/public", file)

这篇关于使用Play Framework资产控制器返回资产时需要添加标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 07:59