Web Api C#端点don';我不担任职务

Web Api C#端点don';我不担任职务,c#,.net,asp.net-web-api,C#,.net,Asp.net Web Api,我对我创建的端点有问题 [Route("api/pag_seguro/transactions/credit_card")] public string DoTransactionWithCreditCard(string senderHash, string cardHash, ProductModels[] products) { bool isSandbox = true; EnvironmentConfiguration.Chang

我对我创建的端点有问题

    [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;
        }
    }
发生的情况是,当我尝试向该端点发送帖子时,它不起作用,它只接受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

添加
[HttpPost]
属性告诉Web API它应该接受POST谓词。

添加
[HttpPost]
属性告诉Web API它应该接受POST谓词。

添加
[HttpPost]

[HttpPost]
[Route("api/pag_seguro/transactions/credit_card")]
public string DoTransactionWithCreditCard(string senderHash, string cardHash, ProductModels[] products)
还要确保您的
RouteConfig
包含
routes.mapmvcattributures()


添加
[HttpPost]

[HttpPost]
[Route("api/pag_seguro/transactions/credit_card")]
public string DoTransactionWithCreditCard(string senderHash, string cardHash, ProductModels[] products)
还要确保您的
RouteConfig
包含
routes.mapmvcattributures()

1) 转到项目中的App_Start目录,修改名为WebApiConfig.css的Web Api配置文件(添加行
config.maphttpAttribute路由();
以启用路由属性)

2) 在WebAPI方法之前添加属性
[Post]

1)转到项目中的App_Start目录,修改名为WebApiConfig.css的Web Api配置文件(添加行
config.maphttpAttribute();
以打开路由属性)


2) 在WebAPI方法之前添加属性
[Post]

您应该定义如下请求实体:

  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";
}

您应该定义如下请求实体:

  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";
}

你把它标在邮局了吗?[HttpPost]属性标签?是的,是的。。。在您要求我这样做之后,我尝试编辑了我的第一个问题。按照惯例,如果您的操作方法有一个复杂类型(类)的参数,它将默认为
post
,否则它将默认为
get
。据我所知,在一个post操作中不能有多个参数,你需要为此创建一个类。尝试将这三个参数提取到类中作为属性,并在操作中接受该类实例。是否将其标记为post?[HttpPost]属性标签?是的,是的。。。在您要求我这样做之后,我尝试编辑了我的第一个问题。按照惯例,如果您的操作方法有一个复杂类型(类)的参数,它将默认为
post
,否则它将默认为
get
。据我所知,在一个post操作中不能有多个参数,你需要为此创建一个类。尝试将这三个参数提取到一个类中作为属性,并在操作中接受该类实例。查看控制台请求URL中的响应:请求方法:POST状态代码:404 Not Found我这样做了,但仍然不工作。查看控制台请求URL中的响应:请求方法:POST状态代码:404 Not Found我这样做了,但仍然不工作。查看控制台请求URL中的响应:localhost:40379/api/pag_seguro/transactions/credit_card Request Method:POST Status Code:404 Not found您是否在web api配置中调用MapHttpAttribute路由()?我这样做了,但仍然不起作用。在控制台请求URL:localhost:40379/api/pag_seguro/transactions/credit_card请求方法:POST状态代码:404找不到响应您是否在web api配置中调用MapHttpAttribute路由()