Add CRUD Endpoints and Consume the Web API
- 4 minutes to read
This topic describes how to create endpoints and test the Web API service. See the following topics for information on how to create a project with the Web API:
- Create a New Application with the Web API.
- Add a Web API Project to an Existing Solution.
- Add the Web API Service to a Blazor Server Project.
Create Endpoints for Business Objects
In the Startup.cs file, add or find the services.AddXafWebApi method call and use the BusinessObject method to create endpoints for business objects. The following code creates endpoints for the ApplicationUser and Contact business objects:
File: MySolution.WebApi\Startup.cs (MySolution.Blazor.Server\Startup.cs)
using MySolution.Module.BusinessObjects;
// ...
namespace MySolution.WebApi {
public class Startup {
// ...
public void ConfigureServices(IServiceCollection services) {
// ...
services.AddXafWebApi(Configuration, options => {
options.BusinessObject<ApplicationUser>();
options.BusinessObject<Contact>();
})
// in XPO applications, uncomment the following line
// .AddXpoServices();
// ...
}
// ...
}
}
Expose or Hide Business Object Properties
ASP.NET Core Web API/OData exposes public business class properties of simple/value types with a setter (writable) in a Web API response. Our Web API Service additionally exposes read-only calculated XPO properties of simple/value types without a setter (readonly) marked with PersistentAliasAttribute.
To hide business class properties from a Web API response at design time, decorate them with IgnoreDataMemberAttribute, if required.
ASP.NET Core Web API/OData does include complex type, reference and collection business class properties in a Web API response by default. To include complex type, reference and collection business class properties in a Web API response, use OData query options:
- Get a Reference Object
- Get an Associated Collection
- Change the Expansion Depth for Related Business Objects
For advanced OData entity model structure customizations, refer to Change an EDM Model Structure using ODataModelBuilder.
Use the Swagger UI to Test the Web API
If your solution includes a Web API project, right-click the project in the Solution Explorer and choose Debug | Start new instance to run the Web API project. A browser displays the page with the available endpoints.
If your solution includes a startup Blazor Server project that contains the Web API, run the application. Add /swagger to the application address (for example, https://localhost:44318/swagger ) and press Enter to display a page with available endpoints.
Refer to the following link for more information on the page’s UI: Swagger UI.
The default configuration starts the Web API service on different ports depending on the project:
Project | Default Port | Example |
---|---|---|
Web API project | 44319 | https://localhost:44319/swagger |
Blazor Server project | 44318 | https://localhost:44318/swagger |
Test the Web API. Expand the GET ApplicationUser endpoint and click the Try it out button. The Execute button is displayed. Click this button to see the result.
Use the Postman Tool to Test the Web API
You can also use the Postman tool to test the Web API. The Postman tool is more flexible and allows you to send complex requests with parameters to the Web API service. Refer to the following link for more information on how to utilize this tool: Sending your first request.
Tip
To test the Web API service hosted on localhost, install the Postman desktop agent as described in the following topic: Installing the Postman desktop agent.
The image below shows a request to the Contact business object filtered by FirstName in the Postman Web UI (https://web.postman.co/home
):
See the following topics for more information on OData query options:
If you enable authentication for your Web API service, specify the authorization settings for the Postman tool. See the following topic for details: Specifying authorization details. If the Web API service uses the XAF JWT token from the “Authenticate” endpoint, copy it from the Swagger UI page as described in the following section: Use the Swagger UI to Test the JWT Authentication.
Consume the Web API from .NET Applications
Refer to the following help article to find the example: Make HTTP Requests to the Web API from .NET Applications.