Skip to main content
A newer version of this page is available. .
All docs
V22.1

Add the Web API Service to a Blazor Server Project

  • 2 minutes to read

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
    • DevExpress.ExpressApp.WebApi.Xpo - in XPO applications only

    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(Configuration, 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.
        })
        // 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");
            });
        }
        // ...
    }
    
  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