WebApi Post转到Get方法
本文关键字:方法 Get 转到 Post WebApi | 更新日期: 2023-09-27 18:22:08
在我的WebApi
中,我正在尝试添加一个POST
方法。我已经有了GET
。添加 POST
方法后,我尝试的每个 HTTP POST
url 都会转到 GetByCatalog
方法。我该如何解决这个问题?可能是路由问题,但我无法解决它。
客户.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; }
}
客户控制器.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;
}
}
客户上下文.cs
public class CustomerContext : DbContext
{
public CustomerContext(string sConnectionString)
: base(sConnectionString)
{
Database.SetInitializer<CustomerContext>(null);
}
public DbSet<Customer> Customers { get; set; }
}
路由配置.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 帖子网址
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 后它工作完美
您在末尾发布的 URL 暗示您实际上并没有使用 POST
来发出请求,而是实际上GET
。您不应在 URL 中包含/Post。Web API 基础结构会为你处理这个问题,如果你改为POST
http://localhost:56909/api/customer!URL 中传递的所有值实际上都应该作为 POST 数据传递,而不是作为查询字符串值传递,并且您确实应该删除 [FromUri]
属性。
我不知道您使用什么框架(如果有的话(从浏览器发出请求,但请确保您POST
-ing 而不是 GET
-ting。
如果您使用的是 jQuery,请在此处查看如何正确执行POST
请求:http://api.jquery.com/jquery.post/
否则,请查看框架的文档。另外,如果您编辑帖子以包含客户端代码(JavaScript(,我可以更新此答案以更符合您的条件。