在Web Api控制器中使用重定向(找到HTTP 302) [英] Use Redirect in Web Api Controller (HTTP 302 Found)
问题描述
由于某种原因,我在尝试找出如何从控制器内部重定向(HTTP 302 Found
)到绝对URL时遇到很多麻烦.
For some reason I am having lots of trouble trying to find out how to redirect (HTTP 302 Found
) to an absolute URL from within a controller.
我已经尝试过了:
this.Redirect("/assets/images/avatars/profile.jpg");
但是我抛出了异常
抛出异常:System.dll中的'System.UriFormatException'
Exception thrown: 'System.UriFormatException' in System.dll
其他信息:无效的URI:无法确定URI的格式.
Additional information: Invalid URI: The format of the URI could not be determined.
我似乎在这里看不到其他所有答案.我正在使用Web API
和MVC 5
.
Every other answer I see on here doesn't seem to be available to me. I am using Web API
and MVC 5
.
推荐答案
对于Redirect
,您需要发送有效 URI
.对于您的情况,如果只想返回relative URI
,则必须告诉它URI class
:
With Redirect
, you need to send a valid URI
. In your case, if you want to return only the relative URI
, you must tell it to URI class
:
public IHttpActionResult Get()
{
return Redirect(new Uri("/assets/images/avatars/profile.jpg", UriKind.Relative));
}
这篇关于在Web Api控制器中使用重定向(找到HTTP 302)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!