2017-12-05 14 views
-1

私はasp.Net MVC5で開発を始めています。
私のMVCプロジェクトでは、URLが別のドメインのURLである文字列URLを返すWebサービスで使用します。C#MVC5現在の文字列のURLを表示

URLに移動します。明確のために

自分が: クライアント形式のホームページを記入し、[送信]を押し、サーバ側では、私は、フォームからのパラメータを持つリクエストウェブ サービスを送信し、私は2番目として提示する必要があり、別のドメインと、このURLでURLを取得しますページをクライアントに返す

public class HomeController : Controller 
{ 
    public ActionResult Home() 
    { 
     return View("~/Views/Home/home.cshtml"); 
    } 

    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult doSomething(Something obj) 
    { 
     //use web service and get string URL 
     string urlString = ;// get from the web service response. 
     return View();// want write in the(); 
    } 
} 
+0

Webサービスの応答から来ているURLをどうしますか。あなたはURLにナビゲートしますか – ankur

+0

これを行うには複数の方法があります。 vビューデータまたはビューバックを使用して、開始することができます。モデルを使用してビューにデータを送信することもできます。 –

+0

はい私が望むもの - @ankur – tal

答えて

1

これはMVCでのナビゲーションにも便利です。

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult doSomething(Something obj) 
{ 
    //use web service and get string URL 
    string urlString = ;// get from the web service response. 

    if (!string.IsNullOrEmpty(urlString)) 
    { 
     //if the url is from within the domain. 
     return RedirectToAction(urlString); 
     //if the url is from other domain use this 
     //return Redirect(urlString); 
    } 

    //If the urlString is empty Return to a error page 
    return View("Error"); 
} 
+0

ありがとうございます。私はそれを確認します – tal

+0

これは、既に存在する 'Action'にリダイレクトするだけであることに注意してください。別のURLにリダイレクトする場合は、別の方法を使用する必要があります。 –

+0

URLは別のサイトの同じドメインではありません。@GeoffJames – tal

0

URLは外部のURLにリダイレクトしたい場合は、Redirect()メソッドを使用する必要があります別のサイトではない、同じドメイン

からです。そのよう

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult DoSomething(Something obj) 
{ 
    // Use web service to get the string URL 
    string urlString = ...; 

    if (string.IsNullOrEmpty(urlString)) 
    { 
     // If the urlString is empty, take the user to an Error View. 
     return View("Error");   
    } 

    // Redirect the user to the urlString 
    return Redirect(urlString); 
} 

私はURLは間違いなく有効であることを確認するために、いくつかのチェックを行うこともお勧めします。 UristaticメソッドIsWellFormedUriString()を使用すると、boolが返されます。そのよう

:あなたが内部のアクションにリダイレクトしている場合@ankurが提案されているよう

また
if (!Uri.IsWellFormedUriString(urlString, UrlKind.Absolute)) 
{ 
    // If the urlString is not a well-formed Uri, take the user to an Error View 
    return View("Error"); 
} 

// Redirect the user to the urlString 
return Redirect(urlString); 

、、、RedirectToAction()メソッドを使用します。

余分な注意点として

あなたのC#メソッド名はPascalCaseを使用していることを確認してください。ローカル変数/プライベートフィールドにはcamelCaseを保存します。

doSomething(...)の代わりにDoSomething(...)を使用します(私の例ではこれを行っています)。

これが役に立ちます。

+0

ありがとうございます..もしそれが働く場合、私は更新されます:) – tal

+0

問題はありません。あなたはそれを整理することを望んでいます。 –

関連する問題