2015-11-23 41 views
0

我不能真正找出代碼中我需要設置我的「等待」,所以它運行我的代碼,以使用戶登錄到該網站。在那裏我需要設置我的等待代碼

問題出在哪裏,當用戶成功登錄網站時,應該在哪裏設置「等待」。

錯誤:

'UserLogin' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'UserLogin' could be found (are you missing a using directive or an assembly reference?)

[HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public async Task<ActionResult> login(UserLogin UserValue) 
    { 
     //Makes database access 
     DataLinqDB db = new DataLinqDB(); 

     if (ModelState.IsValid) 
     { 
      //Is this email and also downloads "salt" from the mail. 
      var email = db.brugeres.FirstOrDefault(i => i.brugernavn == UserValue.Brugernavn); 
      if (email != null) 
      { 
       //Makes password on the SHA256 
       var pass = HelperClass.Helper.Hash.getHashSha256(UserValue.Brugernavn); 

       //Checker up for a username and password match up with what you have written in. 
       //Checks also while on the salt as the few from the past to the fit with "email" 
       var User = db.brugeres.FirstOrDefault(u => u.brugernavn == UserValue.Brugernavn && u.adgangskode == pass && u.salt == email.salt); 
       if (User != null) 
       { 
        .... code her .... 
        //Sending me over to the front. (the closed portion.) 

        await UserValue; //Error here!!! 
        return RedirectToAction("index"); 

       } 
       else 
       { 
        //Error - Invalid username and password do not match. 
        ModelState.AddModelError("", "Unfortunately, it is wrong .."); 
       } 
      } 
      else 
      { 
       //Error - Invalid username do not match. 
       ModelState.AddModelError("", "We could not find you!"); 
      } 
     } 
     return View(); 
    } 

那裏的故障就出在這裏。

//Sending me over to the front. (the closed portion.) 
await UserValue;//Error here! 
return RedirectToAction("index"); 
+1

在該邏輯分支中,似乎沒有任何要等待的東西。 'UserValue'只是一個'UserLogin'對象,不需要等待。 –

+1

實際上,至少從我們這裏可以看出,似乎根本沒有什麼需要等待的東西。 –

+0

@GlorinOakenfoot你完全用你寫的是什麼? /在這裏向我解釋? –

回答

3

UserValue是你的方法參數,爲什麼你想等待?

當您查詢漢堡包表時,您可能會使用await。您可以使用FirstOrDefaultAsync方法,即awaitable

var email = await db.brugeres 
         .FirstOrDefaultAsync(i => i.brugernavn == UserValue.Brugernavn); 
if (email != null) 
{ 
    //Continue with your other code (Password checking) 
    //Make sure you remove the await UserValue line :) 
} 

FirstOrDefaultAsync是在System.Data.Entity命名空間,這是在EntityFramework.dll可用的擴展方法。我檢查的版本是今天最新的版本,即EntityFramework 6.1.3如果您無法訪問此擴展方法,則可能使用舊版本的EF。請從nuget更新到最新的穩定版。

+0

出錯**'表'未包含'FirstOrDefaultAsync'的定義,並且沒有找到接受類型爲'表'的第一個參數的擴展方法'FirstOrDefaultAsync'(您是否缺少using指令或程序集引用? )** –

+0

您使用哪種版本的EF?我認爲,EF 6以後可以使用FirstOrDefaultAsyc擴展方法。 – Shyju

+0

4。6 - 你可以在這裏看到它:http://postimg.org/image/6dxflhdcb/ –