Back to Devexpress

DataSourceLoadOptionsExtensions.ConvertToHttpContent(DataSourceLoadOptionsBase) Method

blazor-devexpress-dot-blazor-dot-datasourceloadoptionsextensions-dot-converttohttpcontent-x28-devextreme-dot-aspnet-dot-data-dot-datasourceloadoptionsbase-x29.md

latest3.3 KB
Original Source

DataSourceLoadOptionsExtensions.ConvertToHttpContent(DataSourceLoadOptionsBase) Method

Generates an HTTP request content based on the specified data loading options.

Namespace : DevExpress.Blazor

Assembly : DevExpress.Blazor.v25.2.dll

NuGet Package : DevExpress.Blazor

Declaration

csharp
public static HttpContent ConvertToHttpContent(
    this DataSourceLoadOptionsBase options
)

Parameters

NameTypeDescription
optionsDevExtreme.AspNet.Data.DataSourceLoadOptionsBase

A DataSourceLoadOptionsBase object that should be converted to HTTP content.

|

Returns

TypeDescription
HttpContent

The HTTP request content.

|

Remarks

The ConvertToHttpContent extension method helps you bind DevExpress Blazor components to a Web API service. The following table lists supported components.

|

Component

|

Property

| | --- | --- | |

DxComboBox<TData, TValue>

|

CustomData

| |

DxListBox<TData, TValue>

|

CustomData

|

Assign the data source type to the component’s T parameter and specify the CustomData property’s delegate that asynchronously loads data from a Web API service. This delegate accepts a DataSourceLoadOptionsBase object as a parameter. Call the ConvertToHttpContent extension method to generate an HTTP request content based on the accepted data loading options.

The following example binds the Data Grid to a Web API service.

razor
@using DevExtreme.AspNet.Data
@using DevExtreme.AspNet.Data.ResponseModel
@using System.Threading
@using System.Threading.Tasks
@using System.Net.Http
@using System.Text.Json

<DxDataGrid T="@WebApiDataSource" CustomData="@LoadCustomData">
    ...
</DxDataGrid>

@code {
    [Inject] protected HttpClient Client { get; set; }

    public class WebApiDataSource {
        // Declare data source fields here.
    }

    protected async Task<LoadResult> LoadCustomData(DataSourceLoadOptionsBase options, CancellationToken cancellationToken) {
        using var response = await Client.PostAsync("https://MyWebApiService", options.ConvertToHttpContent(), cancellationToken);
        response.EnsureSuccessStatusCode();
        using var responseStream = await response.Content.ReadAsStreamAsync();
        return await JsonSerializer.DeserializeAsync<LoadResult>(responseStream, cancellationToken: cancellationToken);
    }
}

See Also

DataSourceLoadOptionsExtensions Class

DataSourceLoadOptionsExtensions Members

DevExpress.Blazor Namespace