Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
308 views
in Technique[技术] by (71.8m points)

c# - How to make GET request with a complex object?

I try to make GET request via WebApi with complex object. Request is like this:

[HttpGet("{param1}/{param2}")]
public async Task<IActionResult> GetRequest(string param1, int param2, [FromBody] CustomObject[] obj)
{
    throw new NotImplementException();
}

Where CustomObject is:

[DataContract]
public class CustomeObject
{        
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public string Email { get; set; }
}

How do I compose a valid GET request?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

[FromBody] CustomObject[] obj ... GET request has no message body and thus you should change it to FromUri.

Sure, take a look at Documentation

public class GeoPoint
{
    public double Latitude { get; set; } 
    public double Longitude { get; set; }
}

public ValuesController : ApiController
{
    public HttpResponseMessage Get([FromUri] GeoPoint location) { ... }
}

Request would be like below, essentially you pass the entire object data as query string

http://localhost/api/values/?Latitude=47.678558&Longitude=-122.130989

An array of object example can be found in another post pass array of an object to webapi


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...