如何构建一个 ASP.Net 核心网络 api Enpoint 来处理带有等号“=”的查询参数
How do I build an ASP.Net core web api Enpoint that handles query parameters with an equal '=' sign
我想使用从这个 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)
);
}
这会产生如下所示的请求:
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
.netcore C# 代码如下所示,正在命中端点,但 manufacturerNameID 始终为 =0,如果我尝试将其更改为字符串,则它为空。
[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;
}
...}
我做错了什么,我希望我的 C# 代码处理这个具有等号的查询参数,因为这似乎是 "HttPClient" class 构建查询参数的标准方式传递了一个 "HttpParams" 对象。我知道如何处理没有 '=' 但我猜 '=' 是新的 standard/best 做法?
您的 api 声明似乎不准确。
这就是我相信你想要做的:
[HttpGet("PartNumber")] /* <--- */
public IEnumerable<Views.ManufacturePartNumber>
PartNumbers(/* --> */[FromQuery]int manufactureNameID)
{...
您希望从 QueryString 中获取参数,因此您在参数旁边使用 [FromQuery]
属性进行了指示,例如:
[FromQuery]int manufactureNameID
此外,您的 [HttpGet]
属性不需要添加任何其他内容,但 "Method Routing" 在本例中为 [HttpGet("PartNumber")]
我想使用从这个 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)
);
}
这会产生如下所示的请求:
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
.netcore C# 代码如下所示,正在命中端点,但 manufacturerNameID 始终为 =0,如果我尝试将其更改为字符串,则它为空。
[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;
}
...}
我做错了什么,我希望我的 C# 代码处理这个具有等号的查询参数,因为这似乎是 "HttPClient" class 构建查询参数的标准方式传递了一个 "HttpParams" 对象。我知道如何处理没有 '=' 但我猜 '=' 是新的 standard/best 做法?
您的 api 声明似乎不准确。
这就是我相信你想要做的:
[HttpGet("PartNumber")] /* <--- */
public IEnumerable<Views.ManufacturePartNumber>
PartNumbers(/* --> */[FromQuery]int manufactureNameID)
{...
您希望从 QueryString 中获取参数,因此您在参数旁边使用 [FromQuery]
属性进行了指示,例如:
[FromQuery]int manufactureNameID
此外,您的 [HttpGet]
属性不需要添加任何其他内容,但 "Method Routing" 在本例中为 [HttpGet("PartNumber")]