无法重定向到Asp.net WebApi工作

时间:2012-12-02 13:23:20

标签: javascript asp.net-web-api

我有一个重定向的webapi控制器:

 var response = this.Request.CreateResponse(HttpStatusCode.Redirect);

 response.Headers.Add("Location",
     "https://www.dropbox.com/1/oauth/authorize?oauth_token=" + pars["oauth_token"]
     + "&oauth_callback=http://localhost:2638/index.html#/tasks/compose-task/");

 return response;

并从javascript中调用它:

    $.ajax("/api/dropbox/get_request_token", { dataType: 'json' })
    .done(function (data)
    {
        alert(ko.toJSON(data));
    })
    .fail(function (err) {alert(ko.toJSON(err));});

网络标签显示:

GET http://localhost:2638/api/dropbox/get_request_token HTTP/1.1
Accept: application/json, text/javascript, */*; q=0.01
Referer: http://localhost:2638/
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.20 (KHTML, like Gecko) Chrome/25.0.1337.0 Safari/537.20

但它永远不会重定向到dropbox.com。相反,它最终会在fail()中结束。

我错过了什么吗?

2 个答案:

答案 0 :(得分:1)

“Location”标题已有一个属性。尝试:

response.Headers.Location = new Uri( "http://google.com");

它对我有用。

答案 1 :(得分:0)

需要将HttpStatusCode设置为Redirect,如下所示:

var response = new HttpResponseMessage(HttpStatusCode.Redirect);

response.Headers.Location = new Uri("http://www.dropbox.com");

return response;

更多信息:http://en.wikipedia.org/wiki/HTTP_302