2017-05-11 3 views
0

Ich bin neu bei Web API im Allgemeinen und ich habe Probleme mit Web API Core, wenn es darum geht, einen neuen Datenbankeintrag mit POST zu erstellen. Der Ajax-Ruf sieht so aus;Web Api Core - kann nicht von der Clientseite aus posten

addSelectedContract = function (contractId, url, callback) { 
     // hard code url to get working 
     url = "http://localhost:17972/api/selectedcontracts"; 
     var contract = JSON.stringify({ contractId: contractId }); 
     $.ajax({ 
      type: "Post", 
      url: url, 
      data: contract, 
      contentType: "application/json" 
     }).done(callback(status)); 
     }; 

Meine API-Methode sieht so aus;

namespace Properties.API.Controllers 
{ 
    [Route("api/selectedcontracts")] 
    public class SelectedContractController : BaseController 
    { 
     private readonly ISirUoW SirUoW; 
     private readonly SirDb Db; 

     public SelectedContractController(SirDb db, ISirUoW repo, Services.IMailService mailService) 
      : base(mailService) 
     { 
      this.Db = db; 
      this.SirUoW = repo; 
     } 

     [HttpPost()] 
     public IActionResult Post([FromBody] SelectedContract contract) 
     { 
      try 
      { 
       this.SirUoW.SelectContract.Add(contract); 
       return Ok(new OperationStatus 
       { 
        Status = true, 
        OperationID = contract.SelectedContractId 
       }); 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine(e); 
       var status = Mapper.Map<OperationStatus>(e); 
       return Ok(status); 
      } 
     } 

Mein Test von Powershell gibt dieses Ergebnis;

Invoke-RestMethod http://localhost:xxxx/api/selectedcontracts -Method POST -Body (@{contractId="7"} | ConvertTo-Json) -ContentType "application/json" 
[ERROR] Invoke-RestMethod : The remote server returned an error: (500) Internal Server Error. 
[ERROR] At line:1 char:1 
[ERROR] + Invoke-RestMethod http://localhost:xxxx/api/selectedcontracts -Method POST -Bod ... 
[ERROR] + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
[ERROR]  + CategoryInfo   : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke- 
[ERROR] RestMethod], WebException 
[ERROR]  + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRes 
[ERROR] tMethodCommand 
[ERROR] 

Die Klasse SelectedContract ist definiert als; Namensraum SIR.Domain.Poco { Verwendung von Schnittstellen; mit System; mit System.ComponentModel.DataAnnotations;

public class SelectedContract : IAuditCreated 
{ 
    [Key] 
    public int SelectedContractId { get; set; } 

    public int ContractId { get; set; } 

    public DateTime Created { get; set; } 

    [MaxLength(50)] 
    public string CreatedBy { get; set; } 
} 

} Ich habe um zum Beispiel Code gesucht, und ich kann nicht sehen, was ich falsch mache.

EDIT: Ich habe eine Änderung vorgenommen, obwohl ich das gleiche Ergebnis bekomme. Die SelectedContract-Klasse ist definiert als;

namespace SIR.Domain.Poco 
    { 
     using Interfaces; 
     using System; 
     using System.ComponentModel.DataAnnotations; 

     public class SelectedContract : IAuditCreated 
     { 
      [Key] 
      public int SelectedContractId { get; set; } 

      public int ContractId { get; set; } 

      public DateTime Created { get; set; } 

      [MaxLength(50)] 
      public string CreatedBy { get; set; } 
     } 
    } 

Ich bin aber nur eine der Felder senden, die ContractID. Ich möchte den Primärschlüssel oder die Prüffelder nicht senden. Also habe ich eine DTO-Klasse erstellt;

Und änderte die Signatur meiner Post-Methode in der API;

 [HttpPost()] 
     public IActionResult Post([FromBody] SelectedContractDto contract) 
     { 
      var selectedContract = new SelectedContract { ContractId = Convert.ToInt32(contract.ContractId) }; 
      try 
      { 
       this.SirUoW.SelectContract.Add(selectedContract); 
       return Ok(new OperationStatus 
       { 
        Status = true, 
        OperationID = selectedContract.SelectedContractId 
       }); 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine(e); 
       var status = Mapper.Map<OperationStatus>(e); 
       return Ok(status); 
      } 
     } 

Immer noch die 500 interne Server Error. Ich hatte einen Blick in Fiddler und fand diesen Fehler auch

An unhandled exception occurred while processing the request. 
InvalidOperationException: 'Microsoft.AspNetCore.Mvc.MvcOptions.InputFormatter c2 s' must not be empty. At least one 'Microsoft.AspNetCore.Mvc.Formatters.IInputFormatter' is required to bind from the body. 
Microsoft.As fdc pNetCore.Mvc.ModelBinding.Binders.BodyModelBinderProvider.GetBinder(ModelBinderProviderContext context) 

ich online etwas über diesen Fehler finden kann.

Antwort

0

Ich nehme an, dass Sie Call-Methode ist aber danach gibt es einen Fehler, der nicht in Try-Catch-Block angezeigt wird? Markieren Sie "Common Language Runtime Exceptions" in Debug -> Windows -> Exception Einstellungen und versuchen Sie es erneut.

Post Ergebnis.

+0

Ich legte einen Haltepunkt in der Methode, aber es erreicht es nicht. – arame3333

+0

Bitte fügen Sie eine Struktur der SelectedContract-Klasse hinzu und aktualisieren Sie Ihre Frage. lesen Sie auch [this] (http://stackoverflow.com/questions/24625303/why-do-we-have-to-specify-frombody-and-fromuri-in-asp-net-web-api) und machen Vergewissern Sie sich, dass Sie [FromBody] ordnungsgemäß verwenden. – lolek

+0

lesen [this] (http://stackoverflow.com/questions/41559050/string-value-is-empty-when-using-frombody-in-asp-net-web-api) und versuchen, ein wenig Ihre Ajax zu manipulieren fordern und verwenden Sie Ihr Vertragsobjekt ohne JSON.stringify() Methode – lolek

Verwandte Themen