Add CRUD Web API Endpoints
- 2 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 not 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.