出于某种原因,我在尝试找出如何从 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 API
和 MVC 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/