Skip to main content
A newer version of this page is available. .
All docs
V21.2

Add the Web API Service to a Blazor Server Project (CTP)

  • 2 minutes to read

Important

We do not recommend that you use the Web API in production code. We made this service available to gather feedback from anyone who considers it for future use.

This topic describes how to add the Web API service to a Blazor Server project.

  1. Install the following NuGet packages to the Blazor Server project (MySolution.Blazor.Server):

    • DevExpress.ExpressApp.WebApi
    • Swashbuckle.AspNetCore

    See the following topic for more information on how to install DevExpress NuGet packages: Install DevExpress Controls Using NuGet Packages.

  2. Use the code below to configure services for the Web API:

    File: MySolution.Blazor.Server\Startup.cs

    using DevExpress.ExpressApp.WebApi.Services;
    using DevExpress.ExpressApp.WebApi.Swashbuckle;
    using Microsoft.OpenApi.Models;
    using Microsoft.AspNetCore.OData;
    // ...
    public void ConfigureServices(IServiceCollection services) {
    // ...     
        services.AddXafWebApi(options => {
            // Use options.BusinessObject<YourBusinessObject>() 
            // to make the Business Object available in the Web API and
            // generate the GET, POST, PUT, and DELETE HTTP methods for it.
        });
        services.AddControllers().AddOData(options => {
            options
                .AddRouteComponents("api/odata", new XafApplicationEdmModelBuilder(services).GetEdmModel())
                .EnableQueryFeatures(100);
        });
        services.AddSwaggerGen(c => {
            c.EnableAnnotations();
            c.SwaggerDoc("v1", new OpenApiInfo {
                Title = "MySolution API",
                Version = "v1",
                Description = @"Use AddXafWebApi(options) in the MySolution.Blazor.Server\Startup.cs file to make Business Objects available in the Web API."
            });
            c.SchemaFilter<XpoSchemaFilter>();
        });
    }
    
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
        if(env.IsDevelopment()) {
            // ...
            app.UseSwagger();
            app.UseSwaggerUI(c => {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "MySolution WebApi v1");
            });
        }
        // ...
    }
    
  3. Optional. Configure authentication settings. The Web API supports all standard ASP.NET Core authentication techniques. See the following topic for more information: Authentication in Web API projects.

  4. Create endpoints and test the Web API.

See Also