简体   繁体   English

如何构建一个 ASP.Net 核心 web api Enpoint 处理带有等号“=”符号的查询参数

[英]How do I build an ASP.Net core web api Enpoint that handles query parameters with an equal '=' sign

I want to take take an endpoint that is built from this Angular typescript frontend code我想采用一个由此 Angular typescript 前端代码构建的端点

  getPartNumbersFromManufacture(manufactureNameId : Number)
  {
    let parameter = new HttpParams().set("FKManufactureNameId", manufactureNameId.toString()) 
    return this.http.get<ManufacturePartNumber[]>(this.manufactureUrl + '/PartNumber', {params: parameter}).pipe(
      tap(data => console.log('All: ' + JSON.stringify(data))),
      catchError(this.handleError)
    );
  }

This results in a request that looks like this:这会产生如下所示的请求:

Request URL: https://localhost:5001/api/Manufacture/PartNumber?FKManufactureNameId=14703
Request Method: GET
Status Code: 200 OK
Remote Address: [::1]:5001
Referrer Policy: no-referrer-when-downgrade

The .netcore C# code looks like this, the endpoint is being hit, but the manufactureNameID is always =0, and if i try to change it to a string it is null. .netcore C# 代码看起来像这样,端点被命中,但制造名称 ID 始终 = 0,如果我尝试将其更改为字符串,则它是 null。

    [Route("api/Manufacture")]
    public class ManufactureController : ControllerBase
    { ...
        [HttpGet("PartNumber/{FKManufactureNameId=manufactureNameID}")]
        public IEnumerable<Views.ManufacturePartNumber> PartNumbers(int manufactureNameID) //pass the manufacture id from the frontend and get the part numbers associated with this manufacturer
        {
            TrackingContext context = new TrackingContext();
            IEnumerable<ManufacturePartNumber> manufacturePartNumbers = context.ManufacturePartNumber.Where(n => n.FkManufactureNameId == manufactureNameID);

            List<Views.ManufacturePartNumber> manufacturePartNumberView = new List<Views.ManufacturePartNumber>();
            for (int i = 0; i < manufacturePartNumbers.Count(); i++)
            {
                manufacturePartNumberView.Add(new Views.ManufacturePartNumber(manufacturePartNumbers.ElementAt(i)));
            }
            return manufacturePartNumberView;
        }
   ...}

What am I doing wrong, I want my C# code to process this query parameter that has an equal sign since this seems to be the standard way that "HttPClient" class builds the query parameters when passed a "HttpParams" object.我做错了什么,我希望我的 C# 代码处理这个具有等号的查询参数,因为这似乎是“HttPClient”class 在传递“HttpParams”ZA8CFDE6331149EB2AC96F8 时构建查询参数的标准方式。 I know how to handle no '=' but I'm guessing the '=' is the new standard/best practice?我知道如何处理 no '=' 但我猜 '=' 是新的标准/最佳实践?

Your api declaration seems it is not accurate.您的 api 声明似乎不准确。

This is is what I believe you want to do:这就是我相信你想要做的:

 [HttpGet("PartNumber")] /* <--- */
 public IEnumerable<Views.ManufacturePartNumber> 
       PartNumbers(/* --> */[FromQuery]int manufactureNameID) 
        {...

You want a parameter to be taken from QueryString therefore you indicate so with [FromQuery] attribute next to the parameter, like:您希望从QueryString中获取参数,因此您可以使用参数旁边的[FromQuery]属性来指示,例如:

[FromQuery]int manufactureNameID

Also your [HttpGet] Attribute does not need to add anything else but the "Method Routing" which in this case is [HttpGet("PartNumber")]此外,您的[HttpGet]属性不需要添加任何其他内容,但“方法路由”在本例中为[HttpGet("PartNumber")]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 ASP.NET Core Web API 参数中发送 URL 参数 - How can I send URL parameter in ASP.NET Core Web API parameters ASP.NET Core 3.1:Web API 身份登录 - ASP.NET Core 3.1: Web API identity sign in 如何在ASP.NET Web API项目而不是.NET Core上使用DI - How do I use DI on asp.net web api project not .net core 如何使用实体框架通过 ASP.NET Core Web API 处理通用查询? - How can I process a generic query through ASP.NET Core Web API using Entity Framework? 如何在不使用EF的情况下连接ASP.NET Core Web API中的数据库? - How do I connect to database in ASP.NET Core Web API without using EF? 如何禁用特定 ASP.NET Core 5.0 Web ZDB974238714CA8DE6347A 操作的自动 model 绑定? - How do I disable automatic model binding for a specific ASP.NET Core 5.0 Web API action? ASP.NET Core 6 Web API - 如何检查给定 ActionDescriptor 的操作是否需要身份验证? - ASP.NET Core 6 Web API - How do I check if the action with given ActionDescriptor requires authentication? 在ASP.NET Core Web Api中具有多个具有多个查询字符串参数的get方法 - Having multiple get-methods with multiple query string parameters in ASP.NET Core Web Api 使用多个查询参数从数据库中获取所有或特定数据(ASP.NET Core Web Api) - Get ALL or SPECIFIC data from the DB using multiple query parameters (ASP.NET Core Web Api) 如何在我上传文件的Asp.Net核心web api端点上进行集成测试? - How to do an integration test on my Asp.Net core web api endpoint where I upload a file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM