Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
299 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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...