从angular 12向asp.net核心web api http get方法发送类objet



我有一个如下所示的web api方法`

[Route("api/[controller]")]
[ApiController]
public class TestController : ControllerBase
{
private readonly ISearchService _searchService;
public TestController(IRequestService searchService)
{
_searchService = searchService;
}
[HttpGet, Route("search")]
public List<ResponseViewModel> search([FromBody] SearchViewModel search)
{
return _searchService.GetSearchResults(search);
}
}`

SearchViewModel.cs

`public class SearchViewModel
{
public int ProductId { get; set; }     
public int StatusId { get; set; }
public int ItemId  { get; set; }
}
`

问题:

我想从angular 12向上面的HttpGet操作方法发送一个SearchViewModel类类型的对象。

通过用[FromBody]装饰search参数,我可以用HttpPost实现这一点。但是有人能告诉我如何使用HttpGet来实现这一点吗。

提前谢谢。

HttpGet不能发布正文。所以你不能用HTTPget方法传递一个对象。但是您可以传递URL查询参数,然后稍后在控制器中捕获它们。

例如您可以通过查询参数传递这些数据。那么你的url可以是这样的查询参数。

http://yourbaseurl/search?ProductId=1&StatusId=34&ItemId=190

然后你可以像这样在c中捕捉params。

public IActionResult YourAction([FromQuery(Name = "ProductId")] string productId,[FromQuery(Name = "StatusId")] string statusId, [FromQuery(Name = "ItemId")] string itemId)
{
// do whatever with those params
}

最新更新