学习009-04 Add Endpoints for Business Object Methods(为业务对象方法添加端点)

Add Endpoints for Business Object Methods(为业务对象方法添加端点)

The Web API Service allows you to automatically generate endpoints for business object methods that are decorated with an ActionAttribute. Such endpoints can accept a required number of parameters and are automatically displayed in the Swagger UI.
Web API Service允许您自动为使用Action属性装饰的业务对象方法生成端点。此类端点可以接受所需数量的参数并自动显示在Swagger UI中。

For general information on how to use the ActionAttribute to generate actions in an XAF application and how to design business object methods so that they can be used with this attribute, refer to the following topic: How to: Create an Action Using the Action Attribute.
有关如何使用ActionAtcade在XAF应用程序中生成操作以及如何设计业务对象方法以便它们可以与此属性一起使用的一般信息,请参阅以下主题:如何:使用Action属性创建操作。

Important
We intentionally disable endpoints for business object methods in our Web API Service for security reasons. Since these methods may make sensitive modifications to data, every Web API Service developer must be cautious and must verify every business object before exposing its methods to consumers. Refer to the Hide Action Methods from Web API section to see how to hide only certain endpoints generated for business object methods.
出于安全原因,我们有意在Web API Service中禁用业务对象方法的端点。由于这些方法可能会对数据进行敏感修改,因此每个Web API Service开发人员都必须谨慎行事,必须在向消费者公开其方法之前验证每个业务对象。请参阅Web API中的隐藏操作方法部分,了解如何仅隐藏为业务对象方法生成的某些端点。

Enable Automatic Generation of Endpoints for Action Methods(启用操作方法的端点自动生成)

To enable automatic generation of endpoints for action methods, specify the WebApiOptions.ConfigureBusinessObjectActionEndpoints delegate in the application’s Statup.cs file. In this delegate, enable the EnableActionEndpoints setting:
要为操作方法自动生成端点,请在应用程序的Statup. cs文件中指定WebApiOptions.ConfigureBusinessObjectActionEndpoints委托。在此委托中,启用EnableActionEndpoints设置:

File: MySolution.WebApi\Startup.cs

C#

services.AddXafWebApi(builder => {
    builder.ConfigureOptions(options => {
        // ...
        options.ConfigureBusinessObjectActionEndpoints(options => {
            options.EnableActionEndpoints = true;
        });
    });
    // ...
});

Additionally, ensure that the MapXafEndpoints method is invoked within the UseEndpoints method call (the XAF Solution Wizard generates the required code automatically).
此外,确保在UseEndpoint方法调用中调用MapXafEndpoint方法(XAF解决方案向导会自动生成所需的代码)。

File: MySolution.WebApi\Startup.cs

C#

public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
    // ...
    app.UseEndpoints(endpoints => {
        // ...
        endpoints.MapXafEndpoints();
    });
}

When the EnableActionEndpoints option is enabled, the Web API Service registers endpoints for all methods decorated with an ActionAttribute. For example, consider the following business object implementation:
启用EnableActionEndpoints选项后,Web API Service会为所有使用ActionAtcade装饰的方法注册端点。例如,请考虑以下业务对象实现:

File: MySolution.WebApi\BusinessObjects\Task.cs

C#

public class Task : BaseObject {
    public virtual string Description { get; set; }
    public virtual bool IsComplete { get; set; }
    public virtual DateTime DueDate { get; set; }

    [Action(Caption = "Postpone a task for N days",
        ToolTip = "Postpone a task. The \"Days\" parameter specifies the number of days the task should be postponed.",
        TargetObjectsCriteria = "Not [IsComplete]")]
    public void Postpone(PostponeParameters parameters) {
        DueDate += TimeSpan.FromDays(parameters.Days);
    }
}

public class PostponeParameters {
    public PostponeParameters() { Days = 1; }
    public uint Days { get; set; }
}

The endpoints generated for the Postpone method are reflected by the Swagger UI as follows:
为Postpone方法生成的端点由Swagger UI反映如下:
在这里插入图片描述

The ActionAttribute’s Caption and ToolTip parameter values are used to fill the endpoint’s summary and description respectively, and the request body example correctly renders the names of the action’s parameters.
动作属性的标题和工具提示参数值分别用于填充端点的摘要和描述,请求正文示例正确呈现动作参数的名称。
在这里插入图片描述

Change the Endpoint Base Path(更改端点基本路径)

Use the BusinessObjectActionEndpointOptions.BasePath option to customize the base URL path for the generated endpoints (the default setting is “/api/odata”). For example:
使用BusinessObjectActionEndpointOptions。BasePath选项自定义生成端点的基本URL路径(默认设置为“/api/odata”)。例如:

File: MySolution.WebApi\Startup.cs

C#

services.AddXafWebApi(builder => {
    builder.ConfigureOptions(options => {
        // ...
        options.ConfigureBusinessObjectActionEndpoints(options => {
            // ...
            options.BasePath = "/my-actions";
        });
    });
    // ...
});

In this configuration, endpoints are generated as shown below:
在此配置中,端点的生成如下所示:
在这里插入图片描述

Hide Action Methods from Web API(从Web API中隐藏操作方法)

Use the BusinessObjectActionEndpointOptions.MethodFilter property to filter out methods that you do not want to be available through Web API endpoints. This property can accept a predicate that takes a MethodInfo object as a parameter. In your implementation of the predicate, you can use the MethodInfo object to decide whether to hide (filter out) specific methods.
使用BusinessObjectActionEndpointOptions. MethodFilter属性过滤掉您不希望通过Web API终结点可用的方法。此属性可以接受将MethodInfo对象作为参数的谓词。在谓词的实现中,您可以使用MethodInfo对象来决定是否隐藏(过滤掉)特定方法。

File: MySolution.WebApi\Startup.cs

C#

services.AddXafWebApi(builder => {
    builder.ConfigureOptions(options => {
        // ...
        options.ConfigureBusinessObjectActionEndpoints(options => {
            // ...
            options.MethodFilter = m => {
                return !m.Name.Contains("MethodToHide");
            };
        });
    });
    // ...
});

Limitations(限制)

The Web API Service does not currently support validation for action method endpoints.
Web API服务当前不支持操作方法端点的验证。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汤姆•猫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值