简体   繁体   中英

WebApi Post goes to Get method

In my WebApi I'm trying to add a POST method. I have the GET already working. With the addition of the POST method every HTTP POST url I try goes to the GetByCatalog method. How can I fix this? Probably a routing issue but I can't manage to fix it.

Customer.cs (Model)

[Table("tblRelatie")]
public class Customer
{
    //pkey
    [DatabaseGenerated(DatabaseGeneratedOption.Identity), Key()]
    [Column("fldRelatieID")]
    public int RelatieId { get; set; }

    [Column("fldRelatieSoort")]
    public int RelatieSoort { get; set; }

    [Column("fldRelatieCode")]
    public int RelatieCode { get; set; }

    [Column("fldNaam")]
    public string Naam { get; set; }

    [Column("fldContactpersoon")]
    public string Contactpersoon { get; set; }

    [Column("fldAdres")]
    public string Adres { get; set; }

    [Column("fldPostcode")]
    public string Postcode { get; set; }

    [Column("fldPlaats")]
    public string Plaats { get; set; }

    //fkey
    [Column("fldLandID")]
    public int? LandId { get; set; }

    [Column("fldCorrespondentieAdresContactpersoon")]
    public string CorrespondentieAdresContactpersoon { get; set; }

    [Column("fldCorrespondentieAdres")]
    public string CorrespondentieAdres { get; set; }

    [Column("fldCorrespondentieAdresPostcode")]
    public string CorrespondentieAdresPostcode { get; set; }

    [Column("fldCorrespondentieAdresPlaats")]
    public string CorrespondentieAdresPlaats { get; set; }

    [Column("fldCorrespondentieAdresLandID")]
    public int? CorrespondentieAdresLandId { get; set; }

    [Column("fldFactuurRelatieID")]
    public int? FactuurRelatieId { get; set; }

    [Column("fldTelefoon")]
    public string Telefoon { get; set; }

    [Column("fldMobieleTelefoon")]
    public string MobieleTelefoon { get; set; }

    [Column("fldFax")]
    public string Fax { get; set; }

    [Column("fldEmail")]
    public string Email { get; set; }

    [Column("fldBtwNummer")]
    public string BtwNummer { get; set; }

    [Column("fldDebiteurennummer")]
    public string Debiteurennummer { get; set; }

    [Column("fldFactuurkorting")]
    public decimal? Factuurkorting { get; set; }

    [Column("fldKrediettermijn")]
    public int? Krediettermijn { get; set; }

    [Column("fldBankrekeningnummer")]
    public string Bankrekeningnummer { get; set; }

    [Column("fldNaamRekeninghouder")]
    public string NaamRekeninghouder { get; set; }

    [Column("fldPlaatsRekeninghouder")]
    public string PlaatsRekeninghouder { get; set; }

    [Column("fldBankieren")]
    public bool Bankieren { get; set; }

    [Column("fldNonactief")]
    public bool Nonactief { get; set; }

    [Column("fldKlantKortinggroepID")]
    public int? KlantKortinggroepId { get; set; }

    [Column("fldKredietLimiet")]
    public decimal? KredietLimiet { get; set; }

    [Column("fldBestelBedragMinimum")]
    public decimal? BestelBedragMinimum { get; set; }

    [Column("fldMemo")]
    public string Memo { get; set; }

    [Column("fldKvkNummer")]
    public string KvkNummer { get; set; }

    [Column("fldCreditcardNummer")]
    public string CreditcardNummer { get; set; }

    [Column("fldWebsiteUrl")]
    public string WebsiteUrl { get; set; }

    [Column("fldAanmanen")]
    public int? Aanmanen { get; set; }

    [Column("fldElektronischFactureren")]
    public bool ElektronischFactureren { get; set; }

    [Column("fldOfferteEmailen")]
    public bool? OfferteEmailen { get; set; }

    [Column("fldOfferteEmailAdres")]
    public string OfferteEmailAdres { get; set; }

    [Column("fldOfferteCcEmailAdres")]
    public string OfferteCcEmailAdres { get; set; }

    [Column("fldBevestigingEmailen")]
    public bool? BevestigingEmailen { get; set; }

    [Column("fldBevestigingEmailAdres")]
    public string BevestigingEmailAdres { get; set; }

    [Column("fldBevestigingCcEmailAdres")]
    public string BevestigingCcEmailAdres { get; set; }

    [Column("fldFactuurEmailAdres")]
    public string FactuurEmailAdres { get; set; }

    [Column("fldFactuurCcEmailAdres")]
    public string FactuurCcEmailAdres { get; set; }

    [Column("fldAanmaningEmailen")]
    public bool? AanmaningEmailen { get; set; }

    [Column("fldAanmaningEmailAdres")]
    public string AanmaningEmailAdres { get; set; }

    [Column("fldAanmaningCcEmailAdres")]
    public string AanmaningCcEmailAdres { get; set; }

    [Column("fldOfferteAanvraagEmailen")]
    public bool? OfferteAanvraagEmailen { get; set; }

    [Column("fldOfferteAanvraagEmailAdres")]
    public string OfferteAanvraagEmailAdres { get; set; }

    [Column("fldOfferteAanvraagCcEmailAdres")]
    public string OfferteAanvraagCcEmailAdres { get; set; }

    [Column("fldBestellingEmailen")]
    public bool? BestellingEmailen { get; set; }

    [Column("fldBestellingEmailAdres")]
    public string BestellingEmailAdres { get; set; }

    [Column("fldBestellingCcEmailAdres")]
    public string BestellingCcEmailAdres { get; set; }

    [Column("fldUblBestandAlsBijlage")]
    public bool UblBestandAlsBijlage { get; set; }

    [Column("fldUblLeverancierNaamHide")]
    public string UblLeverancierNaamHide { get; set; }

    [Column("fldIban")]
    public string Iban { get; set; }

    [Column("fldBic")]
    public string Bic { get; set; }

    [Column("fldIncasseren")]
    public int? Incasseren { get; set; }

}

CustomerController.cs

public class CustomerController : ApiController
{

    /// <summary>
    /// Get all customers from a catalog
    /// </summary>
    /// <param name="catalog">The catalog (administration) name</param>
    /// <returns>All customers in the specified catalog</returns>
    [HttpGet]
    public IEnumerable<Customer> GetByCatalog(string catalog)
    {
        using (var db = new CustomerContext(Extensions.BuildConnectionString(catalog)))
        {
            return db.Customers.ToList();
        }
    }

    [HttpPost]
    public Customer Post(string catalog, [FromUri] Customer customer)
    {
        if (customer == null)
        {
            throw new ArgumentNullException("Customer");
        }

        using (var db = new CustomerContext(Extensions.BuildConnectionString(catalog)))
        {
            db.Customers.Add(customer);
            db.SaveChanges();
            return customer;
        }
    }

CustomerContext.cs

public class CustomerContext : DbContext
{
    public CustomerContext(string sConnectionString)
        : base(sConnectionString)
    {
        Database.SetInitializer<CustomerContext>(null);
    }

    public DbSet<Customer> Customers { get; set; }


}

RouteConfig.cs

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

Http Post url

http://localhost:56909/api/customer/Post?catalog=Kopie&relatiesoort=1&relatiecode=300850&naam=pieter&contactpersoon=pieter&adres=straat&postcode=8794PO&plaats=adam&landid=1&correspondentieadrescontactpersoon=adres&correspondentieadres=straat&correspondentieadrespostcode=8415PO&correspondentieadresplaats=amsterdam&correspondentieadreslandid=1&factuurrelatieid=1&telefoon=0147885665&mobieletelefoon=06559889&fax=0582184&email=test@test.com&btwnummer=688658&debiteurennummer=74498&factuurkorting=12,20&krediettermijn=30&bankrekeningnummer=6554561&naamrekeninghouder=lol&plaatsrekeninghouder=adam&bankieren=true&nonactief=false&klantkortinggroepid=1&kredietlimiet=10,000&bestelbedragminimum=100,00&memo=oude%20klant&kvknummer=665665&creditcardnummer=868658658&websiteurl=test.nl&aanmanen=1&elektronischfactureren=true&offerteemailen=false&offerteemailadres=klant@klant.nl&offerteccemailadres=klant@klant.nl&bevestigingemailen=false&bevestigingemailadres=test@test.com&bevestigingccemailadres=test@test. com&factuuremailadres=test@test.com&factuurccemailadres=test@test.com&aanmaningemailen=false&aanmaningemailadres=test@test.com&aanmaningccemailadres=test@test.com&offerteaanvraagemailen=false&offerteaanvraagemailadres=test@test.com&offerteaanvraagccemailadres=test@test.com&bestellingemailen=false&bestellingemailadres=test@test.com&bestellingccemailadres=test@test.com&ublbestandalsbijlage=false&ublleveranciernaamhide=nee&iban=8688434&bic=8748&incasseren=1

Edit:

I'm using the NuGet Package WebAPITestClient now to do the request it works perfect after the suggested modifications of atornblad

The URL you posted at the end hints that you are not actually using POST to make the request, but actually GET . You should NOT include the /Post in the url. The Web API infrastructure takes care of that for you if you do a POST to http://localhost:56909/api/customer instead! All the values passed in the URL should actually be passed as POST data, and not as query string values, and you really should remove the [FromUri] attribute.

I do not know what framework (if any) you are using to make the request from the browser, but please make sure you are POST -ing and not GET -ting.

IF you are using jQuery, take a look at how to correctly perform a POST request here: http://api.jquery.com/jquery.post/

Otherwise, look at your framework's documentation. Also, if you edit your post to include the client-side code (JavaScript) , I could update this answer to be more in line with your conditions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM