如何验证从 API 重定向是否有效

How to verify if Redirect from API is working

我有一个 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 不包括前面的 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