以错误的请求状态重定向 [英] Redirect with Bad Request Status

查看:88
本文介绍了以错误的请求状态重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Play Framework v2.2.2(scala)并尝试执行状态为400(错误请求)的重定向.

I'm using Play Framework v2.2.2 (scala) and trying to do a Redirect with a 400 (Bad Request) status.

目前,我正在使用反向路由进行重定向,如下所示:

Currently I'm doing redirects using reverse routing like so:

Redirect(Games.Controllers.routes.SportViewController.show)

我希望能够指定重定向状态.我尝试在调用中添加一个附加参数,但出现编译错误:

I'd like to be able to specify the status of the redirect. I tried adding an additional argument to the call, but I get a compile error of:

重载的方法值[重定向]不能应用于(play.api.mvc.Call,Int)

在我发现的文档中,您可以指定具有重定向状态的字符串uri,但是,我真的很想继续在代码中使用反向路由,而不是到处都是字符串uri值.

In the documentation I noticed you can specify a string uri with a redirect status, however, I would really like to continue using reverse routing in my code instead of having string uri values all over the place.

Redirect("redirect/path", status = 400)

关于如何使用反向路由完成Bad Request(400)重定向的任何想法或建议吗?我在文档中错过了什么吗?有一种完全不同/更好的方法吗?

Any ideas or suggestions as how I can accomplish a Bad Request (400) redirect using reverse routing? Is there something I missed in the documentation? Is there a completely different/better way of doing this?

感谢一堆!

要进一步说明我为何询问具有特定状态的重定向.我正在处理一个表单发布.在成功的帖子(例如,用户发布的数据有效)上,然后重定向到路由X,在失败的帖子(例如,用户数据丢失或无效)上,然后重定向到路由Y.在发布不成功的帖子上,应使用什么状态代码用过吗?

To give a bit more context as to why I asked about the redirect with a specific status. I'm processing a form post. On a successful post (eg. user posted data is valid), then redirect to route X, and on unsuccessful post (eg. user data is missing or invalid), then redirect to route Y. On unsuccessful posts, what status code should be used?

推荐答案

这是您感兴趣的Redirect的签名(减去已更改的状态):

This is the signature of the Redirect you're interested in (minus the changed status):

def Redirect(call: Call): SimpleResult = Redirect(call.url) 

它简单地调用了另一个使腿正常工作的重载:

It simply calls another overload that does the leg work:

def Redirect(url: String, queryString: Map[String, Seq[String]] = Map.empty, status: Int = SEE_OTHER)

因此我们可以轻松创建自己的重载,并使用自己的控制器调用上述重载:

So we could easily create our own overload that calls the one above with our own controller:

def Redirect(call: Call, status: Int): SimpleResult = Redirect(call.url, Map.empty, status)

这篇关于以错误的请求状态重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆