【问题标题】: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的情况下如何实现这一点?

问候

【问题讨论】:

    标签: c# asp.net .net asp.net-mvc asp.net-mvc-3


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2022-01-03
      • 1970-01-01
      • 1970-01-01
      • 2013-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-08
      • 1970-01-01
      相关资源
      最近更新 更多