在我的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
编辑:
我现在正在使用NuGet Package
WebAPITestClient
来完成 atornblad
答案 0 :(得分:3)
您在最后发布的网址提示您实际上并未使用POST
发出请求,但实际上是GET
。你应该不在网址中加入/ Post。如果您执行POST
到http://localhost:56909/api/customer,Web API基础架构会为您解决这个问题!在URL中传递的所有值实际上应该作为POST数据传递,而不是作为查询字符串值传递,并且您确实应该删除[FromUri]
属性。
我不知道您使用什么框架(如果有)来从浏览器发出请求,但请确保您POST
- 而不是{{1} } -ting。
IF 您正在使用jQuery,请在此处查看如何正确执行GET
请求:http://api.jquery.com/jquery.post/
否则,请查看您的框架文档。此外,如果您编辑帖子以包含客户端代码(JavaScript),我可以更新此答案,使其更符合您的条件。