[英]Sending class objet to asp.net core web api http get method from angular 12
I've a web api method like below `我有一个 web api 方法,如下所示
[Route("api/[controller]")]
[ApiController]
public class TestController : ControllerBase
{
private readonly ISearchService _searchService;
public RequestController(IRequestService searchService)
{
_searchService = searchService;
}
[HttpGet, Route("search")]
public List<ResponseViewModel> search([FromBody] SearchViewModel search)
{
return _searchService.GetSearchResults(search);
}
}`
SearchViewModel.cs SearchViewModel.cs
`public class SearchViewModel
{
public int ProductId { get; set; }
public int StatusId { get; set; }
public int ItemId { get; set; }
}
`
Question:问题:
I wanted to send an object of type SearchViewModel class to the above Http Get action method from angular 12.我想将 SearchViewModel class 类型的 object 发送到上述 Http 从 ZD18B8624A0F5F721DDFC222 获取操作方法。
I can be able to achieve this with HttpPost by decorating search parameter with [FromBody] .我可以通过使用[FromBody]装饰搜索参数来使用HttpPost来实现这一点。 But can someone please tell me how to do this with HttpGet .但是有人可以告诉我如何用HttpGet做到这一点。
Thanks in advance.提前致谢。
HttpGet cant post a body. HttpGet 无法发布正文。 So you cant pass an object with HTTP get method.所以你不能通过 object 和 HTTP get 方法。 But you can pass URL query params then catch them later in the controller.但是您可以传递 URL 查询参数,然后稍后在 controller 中捕获它们。
such as you can pass those data through query params.例如您可以通过查询参数传递这些数据。 then your url could be like this with query params.那么您的 url 可能与查询参数一样。
http://yourbaseurl/search?ProductId=1&StatusId=34&ItemId=190
then you can catch the params in c# like this.然后你可以像这样捕获 c# 中的参数。
public IActionResult YourAction([FromQuery(Name = "ProductId")] string productId,[FromQuery(Name = "StatusId")] string statusId, [FromQuery(Name = "ItemId")] string itemId)
{
// do whatever with those params
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.