在Web Api控制器中使用重定向(找到HTTP 302) [英] Use Redirect in Web Api Controller (HTTP 302 Found)

查看:444
本文介绍了在Web Api控制器中使用重定向(找到HTTP 302)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我在尝试找出如何从控制器内部重定向(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 APIMVC 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆