简体   繁体   English

WebApi帖子转到Get方法

[英]WebApi Post goes to Get method

In my WebApi I'm trying to add a POST method. 在我的WebApi我尝试添加POST方法。 I have the GET already working. 我的GET已经在工作了。 With the addition of the POST method every HTTP POST url I try goes to the GetByCatalog method. 通过添加POST方法,我尝试了每个HTTP POST URL都转到GetByCatalog方法。 How can I fix this? 我怎样才能解决这个问题? Probably a routing issue but I can't manage to fix it. 可能是路由问题,但我无法解决。

Customer.cs (Model) Customer.cs(模型)

[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 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 CustomerContext.cs

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

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


}

RouteConfig.cs 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发布网址

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. HTTP://本地主机:56909 / API /客户/后目录= Kopie&relatiesoort = 1&relatiecode = 300850&NAAM =彼得&contactpersoon =彼得&ADRES = STRAAT&邮政编码= 8794PO&国家地点=亚当&landid = 1&correspondentieadrescontactpersoon = ADRES&correspondentieadres = STRAAT&correspondentieadrespostcode = 8415PO&correspondentieadresplaats =阿姆斯特丹&correspondentieadreslandid = 1&factuurrelatieid = 1&telefoon = 0147885665&mobieletelefoon = 06559889&传真= 0582184和电子邮件=测试@ test.com&btwnummer = 688658&debiteurennummer = 74498&factuurkorting = 12,20&krediettermijn = 30&bankrekeningnummer = 6554561&naamrekeninghouder =笑与plaatsrekeninghouder =亚当bankieren =真nonactief =假klantkortinggroepid = 1&kredietlimiet = 10,000&bestelbedragminimum = 100,00和备忘录=欧德%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 COM&factuuremailadres =测试@ test.com&factuurccemailadres =测试@ test.com&aanmaningemailen =假aanmaningemailadres =测试@ test.com&aanmaningccemailadres =测试@ test.com&offerteaanvraagemailen =假offerteaanvraagemailadres =测试@ test.com&offerteaanvraagccemailadres =测试@ test.com&bestellingemailen =假bestellingemailadres =测试@ 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 我现在正在使用NuGet Package WebAPITestClient来完成在建议对atornblad进行修改之后可以完美工作的请求

The URL you posted at the end hints that you are not actually using POST to make the request, but actually GET . 您在末尾发布的URL暗示您实际上不是使用POST发出请求,而是使用GET You should NOT include the /Post in the url. 你应该包括/后在URL中。 The Web API infrastructure takes care of that for you if you do a POST to http://localhost:56909/api/customer instead! 如果您改为对http:// localhost:56909 / api / customer进行POST ,则Web API基础结构会为您解决问题! 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. URL中传递的所有值实际上应作为POST数据而不是作为查询字符串值传递,并且您实际上应该删除[FromUri]属性。

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. 我不知道您使用什么框架(如果有)从浏览器发出请求,但是请确保您是POST -ing而不是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/ 如果您使用的是jQuery,请在此处查看如何正确执行POST请求: 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. 另外,如果您编辑帖子以包含客户端代码(JavaScript) ,我可以更新此答案以使其更符合您的条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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