2014-06-24 57 views
0

我想通過POST調用一個REST api並傳入我的參數作爲json數組(用戶名和密碼)。該對象被實例化,但屬性不是。我哪裏錯了?REST api複雜類型參數沒有填充屬性

[HttpPost] 
public AuthenticationResult Authenticate([FromBody]LoginModel login) 
{ 
    try 
    { 
     if (login == null) 
      throw new ArgumentNullException("Username and Password are required for authentication."); 

     AuthenticationService authService = new AuthenticationService(); 
     AuthenticationResult result = authService.IsAuthenticated(login); 

     if (result.IsAuthenticated) 
      return result; 
     else 
     { 
      return new AuthenticationResult() 
      { 
       IsAuthenticated = false, 
       User = new User() 
       { 
        UserId = login.Username 
       } 
      }; 
     } 
    } 
    catch(Exception ex) 
    { 
     return new AuthenticationResult() 
     { 
      IsAuthenticated = false, 
      Message = ex.Message 
     }; 
    } 
} 

而且我從提琴手測試...

我設置爲 「POST」,網址:HTP://本地主機:2134/API /登錄/驗證

請求頭:

User-Agent: Fiddler 
Host: localhost:42761 
Content-Length: 73 
Content-Type: application/json 
Accept: application/json 

請求正文:

{ 
"Username": "ganders", 
"Password": "myHashedPassword" 
} 

編輯:據this article,它會出現,就像我所擁有的一切結構正確...

EDIT2:LoginModel

[Serializable] 
public class LoginModel 
{ 
    public string Username { get; set; } 

    public string Password { get; set; } 

    public string AccessToken { get; set; } 
} 

回答

0

不得不從我LoginModel類刪除[Serializable]屬性。

+1

或者,您可以將屬性'[JsonIgnore]'添加到'AccessToken'中。 – djikay

相關問題