Usar RedirectToAction en la API web

I am using RedirectToAction in my ASP.Net WebAPI application and I tried the following one.

return RedirectToAction("AuthenticateUser", "AuthenticationServiceWebApi", new RouteValueDictionary
                                                                        {"userName", model.UserName},
                                                                        {"password", model.Password}

This generates the redirection as below.

But, since I am using WebAPI, I need to be the URL like below.

¿Cómo hago esto?

preguntado el 31 de julio de 12 a las 10:07

3 Respuestas

If the user is not authenticated you should not redirect. There usually is no interactive user at the other end so you should really return HTTP Status Code 401 instead of redirect.

There is no equivalent in ASP.NET Web API.

Si insistes doing it then here it is:

You throw HttpResponseException:

var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.Found);
httpResponseMessage.Headers.Location = new Uri(myAddress, UriKind.Relative);
throw new HttpResponseException(httpResponseMessage);

Respondido 15 Jul 15, 02:07

I think u did not get it Sir. What I want is to trigger a Web API action using this method. - Thilok Gunawardena

@thilok I also doubt you understood what I meant. But anyway here is an update if you insist doing it. - Alióstad

I'm not sure how your routing, Web API action signature look like so I will try to guess. A few things don't really add up here (why would you pass the password in the url?)

pero ...

Given your url structure I'd guess your routing is something like:

        name: "DefaultApi",
        routeTemplate: "api/{controller}/{action}/{id}/{id2}",
        defaults: new { id = RouteParameter.Optional, id2 = RouteParameter.Optional }

Then given that, I guess your authenticateuser must be something like:

public HttpResponseMessage AuthenticateUser([FromUri]string id, [FromUri]string id2)

If so, then to redirect to this from an MVC controller you need:

        return Redirect(
                new { httproute = "", 
                      controller = "AuthenticationServiceWebApi", 
                      action = "AuthenticateUser", 
                      id = model.UserName,
                      id2 = model.Password

Respondido 31 Jul 12, 11:07

RedirectToAction is MVC not Web API. - Alióstad

Yeah, you can work around that by using Redirect which takes in a string and passing there a Web Api route url build up with UrlHelper. Not pretty but I'm not sure what the OP is trying to achieve. - Filip W.

This is not how API controllers are supposed to be used. There is a reason {action} has to be added here, because API controllers are supposed to have specific purposes and not be filled with lots of actions - USUARIO_8675309

I also came up with a simple solution. Not very good as the above, but worth sharing.

        string post_data = "userName=th&password=admin@123";

        string uri = "";

        // create a request
        HttpWebRequest request = (HttpWebRequest)
        request.KeepAlive = false;
        request.ProtocolVersion = HttpVersion.Version10;
        request.Method = "POST";

        // turn our request string into a byte stream
        byte[] postBytes = Encoding.ASCII.GetBytes(post_data);

        // this is important - make sure you specify type this way
        request.ContentType = "application/x-www-form-urlencoded";
        request.ContentLength = postBytes.Length;
        Stream requestStream = request.GetRequestStream();

        // now send it
        requestStream.Write(postBytes, 0, postBytes.Length);

Gracias a todos.

Respondido 31 Jul 12, 11:07

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.