Poul K. Sørensen
Poul K. Sørensen

Reputation: 17530

Cant get redirect to work in Asp.net WebApi

I have a webapi controller that redirects:

 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;

and call it from javascript:

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

The network tab shows:

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

but it never redirects to dropbox.com. instead it ends up in the fail().

Am i missing something?

Upvotes: 1

Views: 4166

Answers (2)

Wizard of Oz
Wizard of Oz

Reputation: 175

Need to set your HttpStatusCode to Redirect, like so:

var response = new HttpResponseMessage(HttpStatusCode.Redirect);

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

return response;

More info: http://en.wikipedia.org/wiki/HTTP_302

Upvotes: 0

Aliostad
Aliostad

Reputation: 81660

There is already a property for "Location" header. Try:

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

It works for me.

Upvotes: 1

Related Questions