2017-09-28 31 views
0

我有一個非常簡單的登錄頁面。問題出在Login方法中。我執行RedirectToAction,但是,它永遠不會將網頁從登錄頁面更改爲主頁。剃刀重定向包含意外行爲

的HomeController(主要應用控制器)

public ActionResult Index(string username = null, string password = null) 
    { 
     if (username == null || password == null) 
     { 
      return RedirectToAction("Index", "Auth"); 
     } 

     this.username = username; 
     this.password = password; 

     return View(); //and have tried return View("Index"); 
    } 

AuthController(抓住用戶名和密碼)

public ActionResult Login(string username, string password) 
    {   
     return RedirectToAction("Index","Home", new { username=username,password=password}); 
    } 

按鈕按下

function Login() { 
    var _get_searchdetails = '@Url.Action("Login", "Auth")'; 
    $.ajax({ 
    url: _get_searchdetails, 
    data: { username: $('#username').val(), password: $('#password').val() }, 
    type: "POST", 
    beforeSend: function() { 

    }, 
    error: function (xhr, textStatus, error) { 
     alert("Try again!"); 
    }, 
    success: function (htmldata, xhr, settings) { 

    } 
    }); 
} 
+1

'類型:「GET」,'* \ * GASP * *在控制器上設置 – adiga

+0

字段是行不通的,因爲所述控制器被實例化,並設置與每個請求。你做的下一個請求,'username'和'password'將再次爲空。 –

+0

是的,我保存那些...... Session [「username」] = login.username; AND Session [「password」] = login.password;謝謝你! – Rob

回答

0

它實際上並不清楚你所想在這裏做,因爲你的行爲方法代碼沒有意義。

但這裏是更好的選擇可以爲使用RedirecToAction method通過指定控制器和動作名稱與它的參數,如:

return RedirectToAction("Index","Home", new { username=username,password=password}); 

,你不應該使用Get請求通過用戶名和密碼,以行動方法,因爲它很容易被黑客入侵到web應用程序。

希望它有助於!

+0

做了一些更新。我試圖做一個簡單的登錄頁面來抓取用戶憑據來模擬下游。問題是一旦你提到的RedirectToAction被執行,它就會保留在登錄視圖中。 – Rob

0

發現我的問題。我需要這樣做而不是ajax查詢。

 @using (Html.BeginForm("Login", "Auth")) 
     { 
      <label for="username">Username</label> 
      <input type="text" id="username" name="username"/> 
      <br /> 
      <label for="username">Password</label> 
      <input type="password" id="password" name="password"/> 

      <input type="submit" value="Create" /> 
     } 
+0

如果你想保持ajax行爲,你可以在響應中返回一個url,並讓瀏覽器從javascript(類似於'location = response.url')重定向到那個url。但總的來說,在這種情況下,使用ajax並不需要「POST」。 – Extragorey

+0

不,我真的不需要ajax。我只是不確切地知道我在尋找什麼來實現它的工作。我之前從未使用過Razor 5,我試圖編寫代碼並在同一時間學習它,但這並不總能奏效。 – Rob