将 JSON 对象传递给 GET 方法 - ASP.Net ApiController
Passing JSON Object to GET Method - ASP.Net ApiController
我希望在继承 class 的 APIController 中将 URI 字符串传递到 GET 方法中。我试过将它作为原始文本和字符串参数放在内容中,但没有成功。我制作了一个模型作为参数传递:
namespace ServerDock.Models.Web
{
using System.Runtime.Serialization;
[DataContract]
public partial class UriWeb
{
[DataMember]
public string Uri_String { get; set; }
public UriWeb() { }
}
}
现在我的Controller的功能如下:
[Route("file", Name = "Download")]
[HttpGet]
public async Task<IHttpActionResult> DownloadFile([FromBody] UriWeb uri)
用Postman调用whos JSON正文如下:
{
"Uri_String":"https://mysite.windows.net/files/ec0a30c8-265d-4c94-b176-387004f5f566-.jpg"
}
我在控制器函数中的断点从未命中,我收到错误:请求正文不完整。
知道我做错了什么吗?
将 [HttpGet]
更改为 [HttpPost]
。 GET 不能有 [FromBody]
参数:
[Route("file")]
[HttpPost]
public async Task<IHttpActionResult> DownloadFile([FromBody] UriWeb uri){...}
如果必须使用 GET,则需要将其作为查询字符串传递。所以你会把它改成:
[Route("file")]
[HttpGet]
public async Task<IHttpActionResult> DownloadFile([FromQuery] Uri uri){...}
然后这样调用它(查询字符串已经编码):
http://example.com/api/file?uri=http%3A%2F%2Fexample.com%2Ffile.zip
我希望在继承 class 的 APIController 中将 URI 字符串传递到 GET 方法中。我试过将它作为原始文本和字符串参数放在内容中,但没有成功。我制作了一个模型作为参数传递:
namespace ServerDock.Models.Web
{
using System.Runtime.Serialization;
[DataContract]
public partial class UriWeb
{
[DataMember]
public string Uri_String { get; set; }
public UriWeb() { }
}
}
现在我的Controller的功能如下:
[Route("file", Name = "Download")]
[HttpGet]
public async Task<IHttpActionResult> DownloadFile([FromBody] UriWeb uri)
用Postman调用whos JSON正文如下:
{
"Uri_String":"https://mysite.windows.net/files/ec0a30c8-265d-4c94-b176-387004f5f566-.jpg"
}
我在控制器函数中的断点从未命中,我收到错误:请求正文不完整。
知道我做错了什么吗?
将 [HttpGet]
更改为 [HttpPost]
。 GET 不能有 [FromBody]
参数:
[Route("file")]
[HttpPost]
public async Task<IHttpActionResult> DownloadFile([FromBody] UriWeb uri){...}
如果必须使用 GET,则需要将其作为查询字符串传递。所以你会把它改成:
[Route("file")]
[HttpGet]
public async Task<IHttpActionResult> DownloadFile([FromQuery] Uri uri){...}
然后这样调用它(查询字符串已经编码):
http://example.com/api/file?uri=http%3A%2F%2Fexample.com%2Ffile.zip