Passing JSON Object to GET Method - ASP.Net ApiController

Viewed 101

I'm hoping to pass a URI string into a GET method in my APIController inhering class. I've tried putting it in the Content as raw text, and as a string param without luck. I've made a model to pass in as a param:

namespace ServerDock.Models.Web
{
    using System.Runtime.Serialization;

    [DataContract]
    public partial class UriWeb
    {
        [DataMember]
        public string Uri_String { get; set; }

        public UriWeb() { }

    }
}

Now my Controller's function is as follows:

[Route("file", Name = "Download")]
[HttpGet]
public async Task<IHttpActionResult> DownloadFile([FromBody] UriWeb uri)

With a Postman call whos JSON body is as follows:

{
    "Uri_String":"https://mysite.windows.net/files/ec0a30c8-265d-4c94-b176-387004f5f566-.jpg"
}

My breakpoint in the controller function is never hit, and I get the error: The request body is incomplete.

Any idea what I'm doing wrong?

1 Answers

Change [HttpGet] to [HttpPost]. GET's can't have a [FromBody] parameter:

[Route("file")]
[HttpPost]
public async Task<IHttpActionResult> DownloadFile([FromBody] UriWeb uri){...}

If you have to use GET, then you need to pass it as a Query String. So you would change it to:

[Route("file")]
[HttpGet]
public async Task<IHttpActionResult> DownloadFile([FromQuery] Uri uri){...}

And then call it as so (query string has been encoded):

http://example.com/api/file?uri=http%3A%2F%2Fexample.com%2Ffile.zip
Related