【问题标题】:How to verify if Redirect from API is working如何验证来自 API 的重定向是否有效
【发布时间】:2019-11-20 19:34:33
【问题描述】:

我有一个 API 方法需要根据提供的某些输入重定向到 URL。

[ApiController]
public class AuthController : ControllerBase
{
    [HttpGet()]
    public IActionResult GetUrl(string input)
    {
        // retrieve a url based on the input
        string url = GetUrl(input);
        return Redirect(url);
    }
}

当我使用 postman 调试此方法时,我看到检索到了正确的 URL 并调用了 Redirect。但是,在邮递员中,我得到了 HTTP 404 状态。我的问题:

  1. 如何获得一些适当的重定向 HTTP 状态代码?
  2. 从邮递员那里,有什么方法可以验证是否执行了重定向到 URL 的操作?

【问题讨论】:

  • 您想重定向到外部 URL,还是同一主机上的某个 URL?
  • 重定向到外部 URL
  • 我会回答的,我想我找到了你的问题。

标签: postman asp.net-core-webapi http-redirect asp.net-core-3.0


【解决方案1】:

所以,我认为您传递的 URL 前面不包含 http://https://,并且 ASP.NET 试图将您重定向到 http://example.com/url 而不是重定向到外部地点。尝试在传递给 GetUrl 方法的 URL 前添加 https://http://。然后,您应该在 Postman 中获得 200 OK

例子:

using Microsoft.AspNetCore.Mvc;

namespace WebApplication1.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class ExampleController : ControllerBase
    {
        [HttpGet]
        public IActionResult GetUrl([FromQuery]string url)
        {
            return Redirect("https://" + url);
        }
    }
}

在本例中,如果您将 url=google.com 传递给应用程序,您将被重定向到 https://google.com/ 并在 Postman 中获得 200 OK

【讨论】:

    猜你喜欢
    • 2019-01-26
    • 2021-09-14
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    • 2014-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-30
    相关资源
    最近更新 更多