简体   繁体   中英

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

  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.

    [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. I know how to handle no '=' but I'm guessing the '=' is the new standard/best practice?

Your api declaration seems it is not accurate.

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:

[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")]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM