Skip to content

REST Controllers examples

Ushakov Michael edited this page Sep 18, 2025 · 17 revisions

1 Introduction

This page contains examples of how to Create REST Controllers with Wissance.WebApiToolkit.

1.1 ReadOnly REST Controller

This controller class contains the following methods:

  • GET /api/[controller]/?[page={page}&size={size}&sort={sort}&order={order}] to get collection of PagedDataDto<T>
  • GET /api/[controller]/{id} to get one object by id

Consider we have to create the Book Controller with EntityFramework:

  1. Create a book entity and implement the IModelIdentifiable<T> interface.
public class BookEntity : IModelIdentifiable<int>
{
    public int Id {get; set;}
    public string Title {get; set;}
    public string Authors {get; set;} // for simplicity
    public DateTimeOffset Created {get; set;}
    public DateTimeOffset Updated {get; set;}
}

ModelContext Must derive from DbContext and have appropriate DbSet<BookEntity>

public ModelContext : DbContext
{
    // ...
    DbSet<BookEntity> Books {get;set;}
}
  1. Create a DTO - BookDto
public class BookDto
{
    public int Id {get; set;}
    public string Title {get; set;}
    public string Authors {get; set;} 
}
  1. Create a factory function
public static class BookFactory
{
    public static BookDto Create(BookEntity entity)
    {
        return new BookDto
        {
            Id = entity.Id,
            Title = entity.Title,
            Authors = entity.Authors;
        };
    }
}
  1. That's all

Clone this wiki locally