简体   繁体   English

Sending class objet to asp.net core web api http get method from angular 12

[英]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.

相关问题 如何从asp.net核心Web API方法获取角度的不同类型的responseType - How to get different types of responseType in angular from asp.net core web API method 如何从 Angular 中的本地 Web API (ASP.NET Core) 获取数据 - How to get data from local Web API (ASP.NET Core) in Angular 如何将数组从Angular 6传递到ASP.NET Core API的GET方法? - How do I pass array from Angular 6 to ASP.NET Core API for GET method? Angular 4-如何从ASP.Net Web API获取数据 - Angular 4 - How to get data from ASP.Net web api 如何从 ASP.NET Web API 获取 Angular 中的图像? - How to get images in Angular from an ASP.NET Web API? 如何解决混合内容 https & http 与 angular 和 ASP.NET Core Web API 的问题? - How to fix problem of mixed content https & http with angular and ASP.NET Core Web API? 来自ionic 3的ASP.NET Core 2.0 Web API接收/发送Json请求 - ASP.NET Core 2.0 Web API Receiving/Sending Json requests from ionic 3 Angular ASP.NET:Http Get Web Api调用有效,但是Angular没有显示任何值 - Angular ASP.NET: Http Get Web Api call works but no values shown by Angular 如何在 ASP.NET Core 和 Angular 中将 http 参数传递给它时从后端获取 http 标头? - How to get http headers from backend while passing http parameters to it in ASP.NET Core and Angular? 无法将值从angular服务发布到asp.net core angular中的Web API - Unable to post values from angular service to Web API in asp.net core angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM