。而不是空

发布于 2025-01-27 18:46:09 字数 4800 浏览 3 评论 0原文

我正在将实体框架API项目从.NET 5升级到.NET 6,并且在尝试传递字符串参数的无null值时,我对其中一个功能有问题。

作为.NET 6的升级的一部分,所有软件包已更新为最新版本。
Microsoft.aspnetcore.odata从v7.5.6到8.0.10
Microsoft.EntityFrameWorkCore从v5.0.3到6.0.4
如果还有其他包装版本可能相关,请告诉我。

我正在尝试保持API签名并结果相同,以避免需要调整许多消费者。我知道我可以将控制器操作定义为/odata/vpdelemcats(elementID = 1),这可能是当需要为vpdno时定义和调用的方式。

我有一些工作,但是如果可能的话,我正在寻找适当的解决方案。我读过 odata v4 web api中的可选函数参数许多链接已死。

函数定义

var vpdElemCats = builder.Function("VPDElemCats")
  .ReturnsCollection<vPayrollElemCats>();
var paramElemCatVPDNo = vpdElemCats.Parameter<string>("VPDNo");
paramElemCatVPDNo.Optional();
paramElemCatVPDNo.HasDefaultValue(null);
paramElemCatVPDNo.Nullable = true;
vpdElemCats.Parameter<int>("ElementId");

控制器

[HttpGet("/odata/VPDElemCats(VPDNo={vpdNo},ElementId={elementId})")]
public IActionResult VPDElemCats([FromODataUri] string vpdNo, [FromODataUri] int elementId)
{
...

在升级/odata/vpdelemcats(vpdno = null,elementId = 1)之前将VPDNO设置为null,elementID为1和/odata/vpdno(vpdno = '0744444444444444444444444444444444444444444444444444444444444444444444444. 07444) ',elementID = 1)将VPDTO设置为“ 074”,并将ElementID设置为1。

升级后,第一个API调用将VPDNO设置为“ Microsoft.odata.odatanullvalue”。我没想到有几次失败的尝试,但我想知道它们是否可能帮助我朝着正确的方向前进。

我怀疑我的问题要么缺乏对ODATA惯例的了解,而不是与ODATA的错误,但我无法弄清楚为什么在.NET 5中使用的是.NET 6中的工作,或者不支持这一点。由于传递零值的正确方法将是一个可选参数,其签名完全排除了参数。

我的解决方法是:

在1 左右工作

[FromRoute] [frofodatauri] - 但这会影响(我相信 - 尚未确认 -由于目前试图加载时摇摇晃晃的时)如何以夸张的形式记录API。


即使我删除了optional()hasdefaultValue(null)nullable = true lines> hasdefaultValue(null) 。

围绕2

检查如果(vpdno ==“ Microsoft.odata.odatanullvalue”),如果确实如此,请将其设置为null。


我已经尝试了以下没有成功的方法:

失败尝试1

将控制器操作更改为:

[HttpGet("/odata/VPDElemCats(VPDNo={vpdNo},ElementId={elementId})")]
public IActionResult VPDElemCats([FromRoute] int elementId, [FromRoute] string vpdNo = null)

VPDNO仍将“ Microsoft.odata.odatanullvalue”设置为“ vpdno”。

失败的尝试2

将控制器操作更改为:

[HttpGet("/odata/VPDElemCats(VPDNo='{vpdNo}',ElementId={elementId})")]
[HttpGet("/odata/VPDElemCats(VPDNo=null,ElementId={elementId})")]
public IActionResult VPDElemCats([FromRoute] int elementId, [FromRoute] string vpdNo = null)

成功将VPDNO设置为null,但结果集不采用ODATA格式,这意味着消费者需要更改以处理不同格式。 非ODATA格式的示例:

[
  {
    "PayrollId": null,
...

ODATA格式的示例:

{"@odata.context":".../odata/$metadata#Collection(api.vPayrollElemCats)",
  "value":
    [
      {
        "PayrollId":165
...

失败尝试3

将控制器的操作更改为:

[HttpGet("/odata/VPDElemCats(VPDNo={vpdNo},ElementId={elementId})")]
[HttpGet("/odata/VPDElemCats(VPDNo=null,ElementId={elementId})")]
public IActionResult VPDElemCats([FromRoute] int elementId, [FromRoute] string vpdNo = null)

函数定义为:

var vpdElemCats = builder.Function("VPDElemCats")
    .ReturnsCollection<vPayrollElemCats>();
var paramElemCatVPDNo = vpdElemCats.Parameter<string>("VPDNo");
paramElemCatVPDNo.Optional();
paramElemCatVPDNo.Nullable = false;
vpdElemCats.Parameter<int>("ElementId");

传递非null vpd工作,但传递null给出:

odataexception:类型验证失败。预期的不可用的类型“ Edm.String”,但收到了无效的值。

microsoft.odata.odatauriconversionutils.verifyandcoerceuriprimitiveliteral(object primitiveValue,string string trilelalValue,iedmmodel模型,iedmtypereference tepredtypereference) odataexception:请求的参数值(null)无效。参数值应为“ edm.String”类型的格式。

microsoft.aspnetcore.odata.routing.template.template.sementtemplatehelpers.match(odatatemplatemplatemplatetranslatecontext上下文,IEDMFUNTICTARY函数

这一事实是识别vpdno的事实。找到最令人困惑的!

失败的尝试4

作为3尝试,除了允许将VPDNO作为无效的IE paramelemcatvpdno.nullable = true = true;

然后呼吁null vpdno给出:

模棱两可的matchException:请求匹配多个端点。匹配:

es_esrops.controllers.elementscontroller.vpdelemcats(es_esrops) es_esrops.controllers.elementscontroller.vpdelemcats(es_esrops)

I'm in the process of upgrading an Entity Framework API project from .Net 5 to .Net 6 and I'm having issues with one of the functions when trying to pass a null value for a string parameter.

As part of the upgrade to .Net 6 all packages were updated to the latest version.
Microsoft.AspNetCore.OData went from v7.5.6 to 8.0.10
Microsoft.EntityFrameworkCore went from v5.0.3 to 6.0.4
If there are other package versions that might be relevant please let me know.

I'm trying to keep the API signature and results the same, to avoid needing to adjust a number of consumers. I'm aware I can define the controller action to be called as /odata/VPDElemCats(ElementId=1) and this is probably how it should have been defined and called when vpdNo needed to be null.

I have a few work arounds but I'm looking for the proper solution if possible. I have read Optional function argument in OData v4 web api but a number of the links are dead.

Function definition

var vpdElemCats = builder.Function("VPDElemCats")
  .ReturnsCollection<vPayrollElemCats>();
var paramElemCatVPDNo = vpdElemCats.Parameter<string>("VPDNo");
paramElemCatVPDNo.Optional();
paramElemCatVPDNo.HasDefaultValue(null);
paramElemCatVPDNo.Nullable = true;
vpdElemCats.Parameter<int>("ElementId");

Controller Action

[HttpGet("/odata/VPDElemCats(VPDNo={vpdNo},ElementId={elementId})")]
public IActionResult VPDElemCats([FromODataUri] string vpdNo, [FromODataUri] int elementId)
{
...

Prior to the upgrade /odata/VPDElemCats(VPDNo=null,ElementId=1) would set vpdNo to null, elementId to 1 and /odata/VPDElemCats(VPDNo='074',ElementId=1) would set vpdTo to "074" and elementId to 1.

After the upgrade the first API call is setting vpdNo to "Microsoft.OData.ODataNullValue". Several of the failed attempts I wasn't expecting to work but I was wondering if they might help get me headed in the correct direction.

I suspect my issue is either a lack of understanding of OData convention, rather than a bug with OData but I can't figure out why what worked in .Net 5 isn't working in .Net 6, or that this isn't supported as the correct way to pass a null value would be an optional parameter with a signature that excludes the parameter entirely.

The workarounds I have are:

Work Around 1

Change the [FromRoute] to [FromODataUri] - but this affects (I believe - not confirmed yet as swagger is hanging while trying to load at the moment) how the API is documented in swagger.

This works even if I remove the Optional(), HasDefaultValue(null) and Nullable = true lines from the function definition.

Work Around 2

Check if (vpdNo == "Microsoft.OData.ODataNullValue") and, if it does, set it to null.

I've tried the below without success:

Failed Attempt 1

Changing the controller action to:

[HttpGet("/odata/VPDElemCats(VPDNo={vpdNo},ElementId={elementId})")]
public IActionResult VPDElemCats([FromRoute] int elementId, [FromRoute] string vpdNo = null)

vpdNo is still set to "Microsoft.OData.ODataNullValue".

Failed Attempt 2

Changing the controller action to:

[HttpGet("/odata/VPDElemCats(VPDNo='{vpdNo}',ElementId={elementId})")]
[HttpGet("/odata/VPDElemCats(VPDNo=null,ElementId={elementId})")]
public IActionResult VPDElemCats([FromRoute] int elementId, [FromRoute] string vpdNo = null)

Successfully set vpdNo to null but the result set is not in the oData format, which means the consumers need to change to handle the different format.
Example of non-OData format:

[
  {
    "PayrollId": null,
...

Example of OData format:

{"@odata.context":".../odata/$metadata#Collection(api.vPayrollElemCats)",
  "value":
    [
      {
        "PayrollId":165
...

Failed Attempt 3

Changing the controller action to:

[HttpGet("/odata/VPDElemCats(VPDNo={vpdNo},ElementId={elementId})")]
[HttpGet("/odata/VPDElemCats(VPDNo=null,ElementId={elementId})")]
public IActionResult VPDElemCats([FromRoute] int elementId, [FromRoute] string vpdNo = null)

and function definition to:

var vpdElemCats = builder.Function("VPDElemCats")
    .ReturnsCollection<vPayrollElemCats>();
var paramElemCatVPDNo = vpdElemCats.Parameter<string>("VPDNo");
paramElemCatVPDNo.Optional();
paramElemCatVPDNo.Nullable = false;
vpdElemCats.Parameter<int>("ElementId");

passing non-null VPD works but passing null gives:

ODataException: Type verification failed. Expected non-nullable type 'Edm.String' but received a null value.

Microsoft.OData.ODataUriConversionUtils.VerifyAndCoerceUriPrimitiveLiteral(object primitiveValue, string literalValue, IEdmModel model, IEdmTypeReference expectedTypeReference)
ODataException: The parameter value (null) from request is not valid. The parameter value should be format of type 'Edm.String'.

Microsoft.AspNetCore.OData.Routing.Template.SegmentTemplateHelpers.Match(ODataTemplateTranslateContext context, IEdmFunction function, IDictionary<string, string> parameterMappings)

The fact this one is recognising the vpdNo as null rather than a string "Microsoft.OData.ODataNullValue" I find most confusing!

Failed Attempt 4

As attempt 3 except allowing leaving vpdNo as nullable i.e. paramElemCatVPDNo.Nullable = true;

Then calling for null vpdNo gives:

AmbiguousMatchException: The request matched multiple endpoints. Matches:

ES_ESROps.Controllers.ElementsController.VPDElemCats (ES_ESROps)
ES_ESROps.Controllers.ElementsController.VPDElemCats (ES_ESROps)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文