WebApi Post转到Get方法

时间:2015-11-16 11:26:41

标签: c# asp.net-web-api

在我的WebApi我正在尝试添加POST方法。我已经GET了。每POST个网址添加HTTP POST方法,我尝试使用GetByCatalog方法。我怎样才能解决这个问题?可能是路由问题,但我无法解决它。

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

编辑:

我现在正在使用NuGet Package WebAPITestClient来完成 atornblad

的建议修改后完美的请求

1 个答案:

答案 0 :(得分:3)

您在最后发布的网址提示您实际上并未使用POST发出请求,但实际上是GET。你应该在网址中加入/ Post。如果您执行POSThttp://localhost:56909/api/customer,Web API基础架构会为您解决这个问题!在URL中传递的所有值实际上应该作为POST数据传递,而不是作为查询字符串值传递,并且您确实应该删除[FromUri]属性。

我不知道您使用什么框架(如果有)来从浏览器发出请求,但请确保您POST - 而不是{{1} } -ting。

IF 您正在使用jQuery,请在此处查看如何正确执行GET请求:http://api.jquery.com/jquery.post/

否则,请查看您的框架文档。此外,如果您编辑帖子以包含客户端代码(JavaScript),我可以更新此答案,使其更符合您的条件。