1

私のAPIにはGebruikerControllerがあります。 Gebruikerはユーザーのためのもので、このコントローラはユーザーをログインさせ、ユーザーのリストを取得し、ユーザーを追加して特定のユーザーを取得します。しかし、簡単なログイン機能のために独自のカスタムポストメソッドを導入したときに問題が発生しました。私は関数に郵便配達からいくつかのデータを送信するたびに、私は次の応答を取得:ASP.NET Web APIカスタムポストアクションが機能しない

{「ID」:[「値のログインが 'は有効ではありません」]}

私はそれにアクセスこのURLで:

http://localhost:52408/api/gebruikers/login

これは私のコントローラです:

[Produces("application/json")] 
[Route("api/Gebruikers")] 
public class GebruikersController : Controller 
{ 
    private readonly flowerpowerContext _context; 

    public GebruikersController(flowerpowerContext context) 
    { 
     _context = context; 
    } 

    // GET: api/Gebruikers 
    [HttpGet] 
    public IEnumerable<Gebruiker> GetGebruiker() 
    { 
     return _context.Gebruiker; 
    } 

    // GET: api/Gebruikers/5 
    [HttpGet("{id}")] 
    public async Task<IActionResult> GetGebruiker([FromRoute] int id) 
    { 
     if (!ModelState.IsValid) 
     { 
      return BadRequest(ModelState); 
     } 

     var gebruiker = await _context.Gebruiker.SingleOrDefaultAsync(m => m.Id == id); 

     if (gebruiker == null) 
     { 
      return NotFound(); 
     } 

     return Ok(gebruiker); 
    } 

    [Route("api/gebruikers/login")] 
    [HttpPost] 
    public async Task<IActionResult> PostLogin([FromBody] string email, [FromBody] string password) 
    { 
     if(!ModelState.IsValid) 
     { 
      return BadRequest(ModelState); 
     } 

     if(GebruikerVerify(email, password)) 
     { 
      var gebruiker = await _context.Gebruiker.FirstOrDefaultAsync((g) => (g.GebruikerEmail == email && g.GebruikerWachtwoord == password)); 
      return Ok(gebruiker); 
     } 
     else 
     { 
      return BadRequest("invalid data"); 
     } 
    } 

    // PUT: api/Gebruikers/5 
    [HttpPut("{id}")] 
    public async Task<IActionResult> PutGebruiker([FromRoute] int id, [FromBody] Gebruiker gebruiker) 
    { 
     if (!ModelState.IsValid) 
     { 
      return BadRequest(ModelState); 
     } 

     if (id != gebruiker.Id) 
     { 
      return BadRequest(); 
     } 

     _context.Entry(gebruiker).State = EntityState.Modified; 

     try 
     { 
      await _context.SaveChangesAsync(); 
     } 
     catch (DbUpdateConcurrencyException) 
     { 
      if (!GebruikerExists(id)) 
      { 
       return NotFound(); 
      } 
      else 
      { 
       throw; 
      } 
     } 

     return NoContent(); 
    } 

    // POST: api/Gebruikers 
    [HttpPost] 
    public async Task<IActionResult> PostGebruiker([FromBody] Gebruiker gebruiker) 
    { 
     if (!ModelState.IsValid) 
     { 
      return BadRequest(ModelState); 
     } 

     _context.Gebruiker.Add(gebruiker); 
     await _context.SaveChangesAsync(); 

     return CreatedAtAction("GetGebruiker", new { id = gebruiker.Id }, gebruiker); 
    } 

    // DELETE: api/Gebruikers/5 
    [HttpDelete("{id}")] 
    public async Task<IActionResult> DeleteGebruiker([FromRoute] int id) 
    { 
     if (!ModelState.IsValid) 
     { 
      return BadRequest(ModelState); 
     } 

     var gebruiker = await _context.Gebruiker.SingleOrDefaultAsync(m => m.Id == id); 
     if (gebruiker == null) 
     { 
      return NotFound(); 
     } 

     _context.Gebruiker.Remove(gebruiker); 
     await _context.SaveChangesAsync(); 

     return Ok(gebruiker); 
    } 

    private bool GebruikerExists(int id) 
    { 
     return _context.Gebruiker.Any(e => e.Id == id); 
    } 

    private bool GebruikerVerify(string email, string wacthwoord) 
    { 
     if(_context.Gebruiker.Any(e => e.GebruikerEmail == email)) 
     { 
      Gebruiker gebruiker = _context.Gebruiker.FirstOrDefault(e => e.GebruikerEmail == email); 
      if(wacthwoord == gebruiker.GebruikerWachtwoord) 
      { 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 
     else 
     { 
      return false; 
     } 
    } 
} 

次のコードはログインコードです。上記のコードでもご覧になれます。

 [Route("api/gebruikers/login")] 
    [HttpPost] 
    public async Task<IActionResult> PostLogin([FromBody] string email, [FromBody] string password) 
    { 
     if(!ModelState.IsValid) 
     { 
      return BadRequest(ModelState); 
     } 

     if(GebruikerVerify(email, password)) 
     { 
      var gebruiker = await _context.Gebruiker.FirstOrDefaultAsync((g) => (g.GebruikerEmail == email && g.GebruikerWachtwoord == password)); 
      return Ok(gebruiker); 
     } 
     else 
     { 
      return BadRequest("invalid data"); 
     } 
    } 

    private bool GebruikerVerify(string email, string wacthwoord) 
    { 
     if(_context.Gebruiker.Any(e => e.GebruikerEmail == email)) 
     { 
      Gebruiker gebruiker = _context.Gebruiker.FirstOrDefault(e => e.GebruikerEmail == email); 
      if(wacthwoord == gebruiker.GebruikerWachtwoord) 
      { 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 
     else 
     { 
      return false; 
     } 
    } 

私はこれに対してかなり新しく、私はここで間違っていることについては断りません。誰かがこれで私を助けてくれますか?

+0

ルーティングの問題。コントローラのルートプレフィックスのために、 'int 'であると予想される' GetGebruiker'を打っていますが、 'login'が表示されます – Nkosi

+0

次の' FromBody'はアクションパラメータで一度しか使用できません。それらのパラメータを1つのモデルに統合してから 'FromBody'属性を使用する – Nkosi

+0

' login'がPOST要求であると想定されるため、エラーが発生したときに使用されたHTTP動詞を指定する必要があります。 – Nkosi

答えて

5

これはルーティングの問題です。コントローラ上のルートプレフィックスのためにGetGebruikerになると、idintになると予想されますが、それは"login"と表示されます。

次へFromBodyは、アクションパラメータで1回のみ使用できます。それらのパラメータを1つのモデルに統合し、次にFromBody属性を使用します。

public class LoginModel { 
    [Required] 
    public string email { get; set; } 
    [Required] 
    public string password { get; set; } 
} 

マップされたルートを示すためのコメントに注意してください。

[Produces("application/json")] 
[Route("api/Gebruikers")]//route prefix for this controller 
public class GebruikersController : Controller { 
    //...code removed for brevity 

    // GET: api/Gebruikers 
    [HttpGet] 
    public IEnumerable<Gebruiker> GetGebruiker() { 
     //...code removed for brevity 
    } 

    // GET: api/Gebruikers/5 
    [HttpGet("{id:int}")] // Note the route constraint 
    public async Task<IActionResult> GetGebruiker([FromRoute] int id) { 
     //...code removed for brevity 
    } 

    // POST: api/Gebruikers/login 
    [HttpPost("login")] 
    public async Task<IActionResult> PostLogin([FromBody] LoginModel login) { 
     if(!ModelState.IsValid) { 
      return BadRequest(ModelState); 
     } 

     if(GebruikerVerify(login.email, login.password)) { 
      //...code removed for brevity 
     } else { 
      return BadRequest("invalid data"); 
     } 
    } 

    // PUT: api/Gebruikers/5 
    [HttpPut("{id:int}")] 
    public async Task<IActionResult> PutGebruiker([FromRoute] int id, [FromBody] Gebruiker gebruiker) { 
     //...code removed for brevity 
    } 

    // POST: api/Gebruikers 
    [HttpPost] 
    public async Task<IActionResult> PostGebruiker([FromBody] Gebruiker gebruiker) { 
     //...code removed for brevity 
    } 

    // DELETE: api/Gebruikers/5 
    [HttpDelete("{id:int}")] 
    public async Task<IActionResult> DeleteGebruiker([FromRoute] int id) { 
     //...code removed for brevity 
    } 

    //..code removed for brevity 
} 

参照

Routing in ASP.NET Core # Route Constraint Reference

Routing to Controller Actions

Model Binding

+0

ああ[OK]を、私は、このオブジェクトに送信する必要があります: "ログイン":[ \t { \t \t \t "電子メール": "[email protected]"、 \t \t \t "パスワード": "テスト" \t } ] 私は正しいですか? –

+0

@B。オブジェクトのHulshofは '{" email ":" [email protected] "、" password ":" test "}'となります。モデルバインダーは、それをactioパラメーターにマップします。最後のコメントであなたが持っていたのは、配列ではなく、単一のオブジェクト – Nkosi

関連する問題