DevExtreme DxDataGrid是否支持Blazor中的OData



我正在尝试测试这个功能,我在文档中看到了如何在Razor中使用OData,但在Blazor中没有。它是OData supported?

我也在尝试同样的事情,显然不支持开箱即用,但有一个解决方法:

// Step 1: Add the nuget package Microsoft.OData.Client to the Blazor project.
// Step 2: Implement ODataAsyncAdapter
public class ODataAsyncAdapter : DevExtreme.AspNet.Data.Async.IAsyncAdapter
{
private HttpClient httpClient = new HttpClient();
public Task<int> CountAsync(IQueryProvider queryProvider, Expression expr, CancellationToken cancellationToken)
{
DataServiceQueryProvider provider = (DataServiceQueryProvider)queryProvider;
DataServiceQuery query = (DataServiceQuery)provider.CreateQuery(expr);
return this.httpClient.GetFromJsonAsync<int>(query.RequestUri.ToString(), cancellationToken);
}
public Task<IEnumerable<T>> ToEnumerableAsync<T>(IQueryProvider queryProvider, Expression expr, CancellationToken cancellationToken)
{
DataServiceQueryProvider provider = (DataServiceQueryProvider)queryProvider;
return ((DataServiceQuery<T>)provider.CreateQuery<T>(expr)).ExecuteAsync(cancellationToken);
}
}
// Step 3: Register the ODataAsyncAdapter
public static async Task Main(string[] args)
{
DevExtreme.AspNet.Data.Async.CustomAsyncAdapters.RegisterAdapter(typeof(DataServiceQueryProvider), new ODataAsyncAdapter());
}
// Final Step: In the Grid.razor page
<DxDataGrid T="@UserDto" CustomData="@this.LoadCustomData" ShowFilterRow="true" PageSize="10" KeyFieldName="@nameof(UserDto.Id)">
<Columns>
<DxDataGridColumn Field="@nameof(UserDto.Id)" />
<DxDataGridColumn Field="@nameof(UserDto.Name)" />
</Columns>
</DxDataGrid>
@code
{
protected async Task<LoadResult> LoadCustomData(DataSourceLoadOptionsBase options, CancellationToken cancellationToken)
{
options.AllowAsyncOverSync = false;
return await DataSourceLoader.LoadAsync(this.ODataService.CreateQuery<UserDto>("UserDto"), options, cancellationToken);
}
}

附言:我会尽量简化一些事情,他们我会编辑这篇文章。

DxDataGrid组件已经过时,所以我决定发布最新DxGrid组件的解决方案。

我们在GitHub上发布了一个新的分步教程和可运行的示例:如何使用DevExpressBlazor组件为BlazorWebAssembly应用程序创建Web API服务后端。后端使用EF Core进行数据访问,并由免费的DevExpress Web API服务提供CRUD和授权。

该应用程序使用博客文章数据。它使用cookie对用户进行身份验证,确定用户的权限,并选择性地启用以下数据操作:

  • 列出现有的Post记录
  • 显示文章作者的照片
  • 创建新的过帐记录
  • 存档Post记录
  • 显示基于过帐记录的报告

为了将数据从OData/Web API绑定到DxGrid组件,我们的示例实现了由Simple.OData.Client(MIT(提供支持的GridCustomDataSource子代。我们使用了这个开源库,而不是标准的Microsoft OData客户端库,因为后者存在已知的问题(https://github.com/OData/odata.net/issues/1970),在测试时阻止了Blazor WebAssembly的正常操作。

搜索关键字:RBAC、访问控制、身份验证、授权、OData、Swagger、OpenAPI、Web API、ASP.NET Core、Blazor、WebAssembly、WASM、EF Core、EF、实体框架

最新更新