2013-12-10 30 views
2

好的,所以我试图让我的头围绕web api,并且我想创建一个mokup登录页面,它向验证控制器发布用户名和密码。但是,我不似乎在我的岗位要得到任何东西html表单发布到web api 2控制器

Post null

这随后导致问题,当我尝试在我的控制器验证usernaem和密码:

public class LoginCredentials 
{ 
    public string Username { get; set; } 
    public string Password { get; set; } 
} 

public class AuthenticationController : ApiController 
{ 
    /// <summary> 
    /// Creates an authenticated session. If successfully authenticated returns   a 'Set-Cookie' header 
    /// containing the cookie for future requests. On failed login attempt an HTTP 
       401 error is returned. 
    /// </summary> 
    [HttpPost, AllowAnonymous] 
    [Route("api/login")] 
    public HttpResponseMessage Login([FromBody]LoginCredentials credentials) 
    {   
     Logout(); 
     credentials.Username = credentials.Username ?? string.Empty; 
     credentials.Password = credentials.Password ?? string.Empty; 

     var authResult = Authentication.Authenticate(credentials.Username, 
          credentials.Password); 
     if (authResult == null || authResult.User == null) 
      return Authentication.GetUnauthorisedResponse(); 
     if (!authResult.UseAllowed) 
      return Authentication.GetNotUseAllowedResponse 
          (authResult.Status); 

     var authenticationManager = Request.GetOwinContext().Authentication; 
     authenticationManager.SignIn(authResult.User.ToIdentity()); 

     return new HttpResponseMessage(HttpStatusCode.NoContent);; 
    } 
    } 

我的形式从而看起来像:

<form autocomplete="off" method="post" action="/api/login" name="myLogin"> 

    <fieldset> 
     <legend>Login credentials</legend> 

     <div class="form-group"> 
      <label for="Username">Username</label> 
      <input id="Username" type="text" /> 
     </div> 

     <div> 
      <label for="Password">Password</label> 
      <input id="Password" type="password" /> 
      <span class="help-block" data-validation="Password"></span> 
     </div> 

     <div class="form-group center"> 
      <button type="submit" class="btn btn-primary" data-disable-on-submit="true">Log 
       on</button> 
     </div> 
    </fieldset> 
</form> 

不知道我没那么正确。

回答

2

您是否尝试给输入name属性值与视图模型属性名称相同?

<input id="Username" name="Username" type="text" /> 

这会将表单发布的数据绑定到视图模型对象。

+1

宾果,名称属性工作的一种享受。干杯 – CSharpNewBee

相关问题