ODATA-如何在Odatamodelbuilder ActionConfiguration中定义参数格式

发布于 2025-02-01 10:54:32 字数 790 浏览 2 评论 0原文

使用.NET CORE 3.1中的ODATA4,我创建了一个操作来插入某些实体,并在操作中添加了一个附加参数:

public void Apply(ODataModelBuilder builder, ApiVersion apiVersion)
{
    var ac = builder.EntityType<MyThing>().Collection.Action("BulkAddMyThing");
    ac.Parameter(typeof(datetime), "RefDate").Required();
    ac.CollectionParameter<MyThing>("MyThings");

这会导致摇摇晃晃的请求结构,看起来像这样:

{
  "RefDate": "2022-05-24T16:05:34.050Z",
  "MyThings": [
    {
      "Name": "string",
      "Rank": "string",
      "Level": "string",
    }
  ]
}

我想做的是BE能够将倒退为日期,而没有时间/时区,但是我不知道如何在弹药参数上定义任何类型的验证或默认值,以便Swagger Doc显示“ 2022-05-24”例如。我认为一个字符串,而不是带有正则表达式的DateTime作为替代方案,但是在Swagger Doc中指定示例输入时仍然很难。

Using Odata4 in .net core 3.1, I have created an action to Bulk insert some entities, and added an additional parameter to the action:

public void Apply(ODataModelBuilder builder, ApiVersion apiVersion)
{
    var ac = builder.EntityType<MyThing>().Collection.Action("BulkAddMyThing");
    ac.Parameter(typeof(datetime), "RefDate").Required();
    ac.CollectionParameter<MyThing>("MyThings");

which results in a request structure in swagger that looks like this:

{
  "RefDate": "2022-05-24T16:05:34.050Z",
  "MyThings": [
    {
      "Name": "string",
      "Rank": "string",
      "Level": "string",
    }
  ]
}

What I'd like to do is be able to force the RefDate to be a date, without the time/timezone, but I can't figure out how to define any kind of validation or default values on the RefDate parameter so that the swagger doc shows "2022-05-24" for example. I considered a string instead of a datetime with a regex as an alternative, but I'd still have trouble specifying the example input in the swagger doc.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

他是夢罘是命 2025-02-08 10:54:32

您应该使用其他类型,而不是dateTimemicrosoft.odata.edm.date
在您的情况下:

ac.Parameter(typeof(Microsoft.OData.Edm.Date), "RefDate").Required();

You should use different type, instead of DateTime: Microsoft.OData.Edm.Date
in your case:

ac.Parameter(typeof(Microsoft.OData.Edm.Date), "RefDate").Required();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文