2012-07-31 10 views
5

私はASP.Net WebAPIアプリケーションでRedirectToActionを使用しています。Web APIでRedirectToActionを使用

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

これにより、次のようなリダイレクトが生成されます。

127.0.0.1:81/authenticationservicewebapi/authenticateuser/admin/[email protected] 

しかし、私はWebAPIを使用しているため、以下のようなURLにする必要があります。

127.0.0.1:81/api/authenticationservicewebapi/authenticateuser/admin/[email protected] 

どうすればよいですか?

答えて

3

私はあなたのルーティング、ウェブAPIアクション署名は、私がしようとしますので、どのように見えるかわからないんだけど推測する。 いくつかのことが本当に私はあなたのルーティングのようなものだと思いたい、あなたのURL構造を考えると

(なぜあなたは、URLにパスワードを渡すのでしょうか?)ここ

を追加しますが...いない:

もしそうなら、あなたは、NE MVCコントローラからこれにリダイレクトするために、

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

:私はあなたのauthenticateUserのようなものでなければならないと思い、与えられたことを次に

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

ed:

 return Redirect(
      Url.RouteUrl("DefaultApi", 
       new { httproute = "", 
         controller = "AuthenticationServiceWebApi", 
         action = "AuthenticateUser", 
         id = model.UserName, 
         id2 = model.Password 
      })); 
+12

'RedirectToAction'はMVCではないWeb APIです。 – Aliostad

+0

ええ、リダイレクトを使用することで回避できます。リダイレクトは文字列を受け取り、UrlHelperで構築したWeb ApiルートURLを渡します。かなりではありませんが、私はOPが何を達成しようとしているのか分かりません。 –

+0

うまく動く、ありがとう。 – thilok

4

ユーザーが認証されていない場合は、リダイレクトしないでください。通常、リダイレクトの代わりにHTTPステータスコード401を返さなければならないので、相手側には対話ユーザーはいません。


ASP.NET Web APIには同等の機能はありません。

あなたが主張する場合それはその後、ここでそれをやって:

あなたはHttpResponseExceptionスロー:

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

私はそれを得ていないと思う。私が望むのは、このメソッドを使用してWeb APIアクションをトリガーすることです。 – thilok

+0

@ティロック私はまたあなたが私が何を意味したのか疑問に思います。しかし、とにかくここにあなたがそれを行うとしている場合は更新です。 – Aliostad

0

また、私は簡単な解決策を考え出しました。上記のようにあまり良くないが、分かち合う価値がある。

 string post_data = "userName=th&[email protected]"; 

     string uri = "http://127.0.0.1:81/api/authenticationservicewebapi/authenticateuser"; 

     // create a request 
     HttpWebRequest request = (HttpWebRequest) 
     WebRequest.Create(uri); 
     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); 
     requestStream.Close(); 

ありがとうございました。

関連する問題