Integrate the Web API into an Existing XAF Blazor Application(将Web API集成到现有的XAF Blazor应用程序中)
This topic describes how to add the Web API service to a Blazor Server project.
本主题介绍如何将Web API服务添加到Blazor Server项目。
Add Web API Service: Core Functionality(添加Web API服务:核心功能)
Install NuGet Packages(安装NuGet包)
Install the following NuGet packages to the Blazor Server project (MySolution.Blazor.Server):
将以下NuGet包安装到Blazor Server项目(MySolutions. Blazor.Server):
- DevExpress.ExpressApp.WebApi
- Swashbuckle.AspNetCore
- DevExpress.ExpressApp.WebApi.Xpo - XPO applications only(仅限XPO应用程序)
See the following topic for more information on how to install DevExpress NuGet packages: Choose Between Offline and Online DevExpress NuGet Feeds.
有关如何安装DevExpress NuGet包的更多信息,请参阅以下主题:在离线和在线DevExpress NuGet Feeds之间进行选择。
Add Service Configuration Code(添加服务配置代码)
Use the code below to configure services for the Web API:
使用下面的代码为Web API配置服务:
File: MySolution.Blazor.Server\Startup.cs
C#
using DevExpress.ExpressApp.WebApi.Services;
using Microsoft.OpenApi.Models;
using Microsoft.AspNetCore.OData;
// ...
public void ConfigureServices(IServiceCollection services) {
// ...
services.AddXafWebApi(Configuration, options => {
// To make a Business Object available via Web API and
// to generate GET, POST, PUT, and DELETE HTTP methods for it,
// uncomment the following line.
// !!!
// options.BusinessObject<YourBusinessObject>();
// !!!
})
// In EF Core applications, do nothing.
// In XPO applications, uncomment the following line.
// !!!
// .AddXpoServices()
// !!!
;
services.AddControllers().AddOData((options, serviceProvider) => {
options
.AddRouteComponents("api/odata", new EdmModelBuilder(serviceProvider).GetEdmModel())
.EnableQueryFeatures(100);
});
services.AddSwaggerGen(c => {
c.EnableAnnotations();
c.SwaggerDoc("v1", new OpenApiInfo {
Title = "MySolution API",
Version = "v1",
Description = @"Use AddXafWebApi(Configuration, options) in the MySolution.Blazor.Server\Startup.cs file to make Business Objects available in the Web API."
});
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
if(env.IsDevelopment()) {
// ...
app.UseSwagger();
app.UseSwaggerUI(c => {
c.SwaggerEndpoint("/swagger/v1/swagger.json", "MySolution WebApi v1");
});
}
// ...
}
Configure Authentication Settings (Optional)(配置身份验证设置(可选))
The Web API supports all standard ASP.NET Core authentication techniques. See the following topic for more information: Authentication in Web API projects.
Web API支持所有标准ASP.NET核心身份验证技术。有关详细信息,请参阅以下主题:Web API项目中的身份验证。
Create Endpoints and Test the Web API(创建端点并测试Web API)
You can now create endpoints and test the Web API.
您现在可以创建端点并测试Web API。
Add Web API Service: Additional Modules(添加Web API服务:附加模块)
You can enable additional Web API Service modules in an existing application. For instructions, please refer to the topic that describes the required module:
您可以在现有应用程序中启用其他Web API服务模块。有关说明,请参阅描述所需模块的主题:
- Obtain a Report from a Web API Controller Endpoint(从Web API控制器端点获取报告)
- Validate Data Sent to Web API Endpoints(验证发送到Web API端点的数据)
- Audit Trail: Log Data Changes Made via Web API Endpoints(审计跟踪:通过Web API端点进行的日志数据更改)
A number of modules are available to you right away, without the need for manual registration:
许多模块可供您立即使用,无需手动注册:
- Obtain BLOB Data from a Web API Controller Endpoint(从Web API控制器端点获取BLOB数据)
- Execute CRUD Operations for Non-Persistent Objects(对非持久性对象执行CRUD操作)
- Obtain Localization Strings from a Web API Controller Endpoint(从Web API控制器端点获取本地化字符串)
Note
Additional modules listed above ship only as part of the DevExpress Universal Subscription.
上面列出的其他模块仅作为DevExpress通用订阅的一部分提供。