重定向返回“对象移动到”

Posted

技术标签:

【中文标题】重定向返回“对象移动到”【英文标题】:Redirect returns "Object moved to" 【发布时间】:2015-09-04 22:41:20 【问题描述】:

我有问题。当我尝试从 MVC 操作将用户重定向到非 http URL 时,它返回:

对象已移至此处。

完整响应(来自 Fiddler):

HTTP/1.1 301 Moved Permanently
Cache-Control: private
Content-Type: text/html; charset=utf-8
Location: myGame-app://test.somespecificdata
Server: Microsoft-IIS/8.0
X-AspNetMvc-Version: 4.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcUGlvdHJcRGVza3RvcFxUYWtlc0NhcmVcVGFrZXNDYXJlXERvY3RvcnNcRWRvY3RvclxDb25zdWx0YXRpb25cMTkx?=
X-Powered-By: ASP.NET
Date: Thu, 18 Jun 2015 18:19:35 GMT
Content-Length: 457

<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="myGame-app%3a%2f%2ftestprotocol.somespecificdata">here</a>.</h2>

<!-- Visual Studio Browser Link -->
<script type="application/json" id="__browserLink_initializationData">
    "appName":"Firefox"
</script>
<script type="text/javascript" src="http://localhost:53098/4c9c75263d91451fa797f9041e4bd0f3/browserLink" async="async"></script>
<!-- End Browser Link -->

</body></html>

我的操作(带有伪代码):

[HttpGet]
public ActionResult Consultation(int id)

      //.. specific business logic
      if(IsMobile())
          return RedirectPermanent("myGame-app://test.somespecificdata"); 
      
      else
          return View("AboutGame", SomeSpecificModel);
      

同样的情况是return Redirect()而不是return RedirectPermanent()

我的主要目标是将使用移动浏览器的用户重定向到具有特殊协议(不是 http)的 URL。这个特殊协议 (myGame-app://) 运行我的移动应用程序according to this Stack discussion。在没有信息Object moved to here的情况下如何实现这一点?

问候

【问题讨论】:

【参考方案1】:

Redirect/RedirectPermanent 方法将正文添加到包含“已移至此处”-HTML 的响应中。 如果您想要一个“空”响应,只需自己设置响应:

HttpContext.Current.Response.Redirect("url", false);
HttpContext.Current.Response.StatusCode = 301;
HttpContext.Current.ApplicationInstance.CompleteRequest();

请注意,在此之后的其余逻辑将被执行,因此请确保在设置此设置后没有其他可能再次重定向响应的(过滤器等)发生。

【讨论】:

以上是关于重定向返回“对象移动到”的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 重定向返回导致重定向循环

如何在nginx创建临时重定向和永久重定向

django重定向导致重定向到下一页,但然后返回原始?

JS - 重定向并返回主页

HTTP重定向

php sdk 的 Facebook 'redirect_uri' 参数未重定向到传递的 uri(仅重定向返回主页)