M
M
Max Maximov2017-05-05 12:50:12
ASP.NET
Max Maximov, 2017-05-05 12:50:12

How to accept model from Get request in WebAPI 2?

Hello!
Project: ASP.NET WebAPI 2
Required to accept from a GET string of type: URL?txn_id=20193504262040&txn_date=1491471490074&account=7013954927&trm_id=952
there is a class:

public class Payment
    {
        public long Id { get; set; }
        public long TxnId { get; set; }
        public DateTime TxnDate { get; set; }
        public string Account { get; set; }
        public long TrmId { get; set; }
        public long PrvId { get; set; }
        public string ProviderName { get; set; }
        public int TrmType { get; set; }
        public double FromAmount { get; set; }
        public double ToAmount { get; set; }
    }

with controller:
public IHttpActionResult Get([FromUri] Payment payment)
        {}

Doesn't map query string variables in c to the Payment class. I have already specified the DataMember, JsonPropertyName and so on attributes. Does not work.
Only this option works:
public IHttpActionResult Get([FromUri(Name = "txn_id")] long txnId,
                                    [FromUri(Name = "txn_date")] string txnDate,
                                    [FromUri(Name = "account")] string account,
                                    [FromUri(Name = "trm_id")] long trmId,
                                    [FromUri(Name = "prv_id")] long prvId,
                                    [FromUri(Name = "trm_type")] int trmType,
                                    [FromUri(Name = "prv_name")] string prvName,
                                    [FromUri(Name = "from_amount")] double fromAmount,
                                    [FromUri(Name = "to_amount")] double toAmount)
{}

But it is too complicated and ugly.
Any suggestions, examples, or tips on where to dig?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Peter, 2017-05-05
@petermzg

How do you want this to work?
If your names don't match, how is the computer supposed to know that "txn_date" is txnDate and so on.
Here is an option for you.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question