c# - 在 Web Api Controller 中使用重定向(发现 HTTP 302)

标签 c# asp.net-mvc redirect asp.net-web-api http-status-code-302

出于某种原因,我在尝试找出如何从 Controller 中重定向(HTTP 302 Found)到绝对 URL 时遇到了很多麻烦。

我试过这个:

this.Redirect("/assets/images/avatars/profile.jpg");

但是我抛出了一个异常

Exception thrown: 'System.UriFormatException' in System.dll

Additional information: Invalid URI: The format of the URI could not be determined.

我在这里看到的所有其他答案似乎都对我不可用。我正在使用 Web APIMVC 5

最佳答案

使用重定向,您需要发送一个有效 URI。在您的情况下,如果您只想返回相对 URI,您必须将它告诉 URI 类:

public IHttpActionResult Get()
{
    return Redirect(new Uri("/assets/images/avatars/profile.jpg", UriKind.Relative));
}

关于c# - 在 Web Api Controller 中使用重定向(发现 HTTP 302),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40386554/

相关文章:

c# - WPF:将 ListView 与多个源绑定(bind)

c# 重写 url 参数

c# - Web.Config AppSettings 中的 Json

.htaccess - htaccess - 将目录重定向到同名文件

php - 返回上一页,带标题( "Location: ");在 PHP 中

c# - 没有属性的C#XmlDocument SelectSingleNode

c# - 使用 .NET 重新启动 Windows 应用程序

javascript - 在项目单击上渲染 View 模型

CSS 模板和 visual studio MVC 元素

php - 在 AngularJS 页面的 .htaccess 中重定向机器人和爬虫