我遇到了我创建的终点问题
[Route("api/pag_seguro/transactions/credit_card")]
public string DoTransactionWithCreditCard(string senderHash, string cardHash, ProductModels[] products)
{
bool isSandbox = true;
EnvironmentConfiguration.ChangeEnvironment(isSandbox);
// Instantiate a new checkout
CreditCardCheckout checkout = new CreditCardCheckout();
// Sets the payment mode
checkout.PaymentMode = PaymentMode.DEFAULT;
// Sets the receiver e-mail should will get paid
checkout.ReceiverEmail = "financeiro@proteste.org.br";
// Sets the currency
checkout.Currency = Currency.Brl;
// Add items
checkout.Items.Add(new Item("0001", "Garrafa Laranja Tupperware", 1, 130.98m));
// Sets a reference code for this checkout, it is useful to identify this payment in future notifications.
checkout.Reference = "REFPT0002";
// Sets shipping information.
checkout.Shipping = new Shipping();
checkout.Shipping.ShippingType = ShippingType.Sedex;
checkout.Shipping.Cost = 0.00m;
checkout.Shipping.Address = new Address(
"BRA",
"SP",
"Sao Paulo",
"Jardim Paulistano",
"01452002",
"Av. Brig. Faria Lima",
"1384",
"5o andar"
);
// Sets a credit card token. -- gerado em 06/03/2017
checkout.Token = cardHash;
//Sets the installments information
checkout.Installment = new Installment(1, 130.98m, 2);
// Sets credit card holder information.
checkout.Holder = new Holder(
"Holder Name",
new Phone("11", "56273440"),
new HolderDocument(Documents.GetDocumentByType("CPF"), "12345678909"),
"01/10/1980"
);
// Sets shipping information.
checkout.Billing = new Billing();
checkout.Billing.Address = new Address(
"BRA",
"SP",
"Sao Paulo",
"Jardim Paulistano",
"01452002",
"Av. Brig. Faria Lima",
"1384",
"5o andar"
);
// Sets your customer information.
// If you using SANDBOX you must use an email @sandbox.pagseguro.com.br
checkout.Sender = new Sender(
"Diogo Amaral",
"comprador@sandbox.pagseguro.com.br",
new Phone("21", "992947883")
);
checkout.Sender.Hash = senderHash;
SenderDocument senderCPF = new SenderDocument(Documents.GetDocumentByType("CPF"), "12345678909");
checkout.Sender.Documents.Add(senderCPF);
try
{
AccountCredentials credentials = PagSeguroConfiguration.Credentials(isSandbox);
Transaction result = TransactionService.CreateCheckout(credentials, checkout);
//return result.TransactionStatus.ToString();
return result.Code.ToString();
}
catch (PagSeguroServiceException exception)
{
string errorstr = "";
foreach (Uol.PagSeguro.Domain.ServiceError erro in exception.Errors)
{
errorstr += erro.ToString();
}
return exception.Message + " - code: " + exception.StatusCode.ToString() + " - errors: " + exception.Errors.ToString() + " - errorstr: " + errorstr;
}
}
当我尝试向此端点发送POST时,它不起作用,它只接受GET。我应该怎么做才能成为POST端点?
我是新手,请帮帮我们。谢谢!
现在我的代码看起来像这样:
[HttpPost]
[Route("api/pag_seguro/transactions/credit_card")]
public string DoTransactionWithCreditCard(string senderHash, string cardHash, ProductModels[] products)
{
bool isSandbox = true;
EnvironmentConfiguration.ChangeEnvironment(isSandbox);
// Instantiate a new checkout
CreditCardCheckout checkout = new CreditCardCheckout();
...
}
但是在控制台中我仍然在Chrome控制台中收到404 Not found错误:
Request URL:http://localhost:40379/api/pag_seguro/transactions/credit_card
Request Method:POST
Status Code:404 Not Found
答案 0 :(得分:1)
添加[HttpPost]
属性以告知Web API它应该接受POST动词。
答案 1 :(得分:1)
添加[HttpPost]
:
[HttpPost]
[Route("api/pag_seguro/transactions/credit_card")]
public string DoTransactionWithCreditCard(string senderHash, string cardHash, ProductModels[] products)
另请确保您的RouteConfig
包含routes.MapMvcAttributeRoutes()
:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
//Other stuff
routes.MapMvcAttributeRoutes(); //make sure this is there
}
}
答案 2 :(得分:0)
1)转到项目中的App_Start目录并修改名为WebApiConfig.css的Web Api配置文件(添加行config.MapHttpAttributeRoutes();
以启用Route属性)
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
2)在WebAPI方法之前添加属性[Post]
。
答案 3 :(得分:0)
您应该将Request实体定义为:
public class PayRequest {
public string senderHash;
public string cardHash;
public ProductModels[] products;
}
您可以使用字段定义属性。 你的方法应该是这样的:
[HttpPost]
[Route("api/pag_seguro/transactions/credit_card")]
public string DoTransactionWithCreditCard(PayRequest request)
{
// ...
return "ok";
}