saule icon indicating copy to clipboard operation
saule copied to clipboard

How to resolve 'Resource types must inherit from Saule.ApiResource'

Open abhalesd opened this issue 3 years ago • 3 comments

I have added package reference of saule in my project. But I am not able to resolve this error occurring after that

Resource types must inherit from Saule.ApiResource

Can you please help me in resolving this?

abhalesd avatar Oct 19 '22 11:10 abhalesd

Did you follow the "getting started" guide? It explains what to do: https://joukevandermaas.github.io/saule/content/1-getting-started.html

joukevandermaas avatar Oct 19 '22 13:10 joukevandermaas

Hi, Thanks for replying. I have followed most of the instructions.

  1. config.ConfigureJsonApi(); is added to WebAPiConfig.cs
  2. Created resource as follow
public class BuidlingApiResources : ApiResource
    {
        BuidlingApiResources() {
            Attribute(nameof(BuildingDto.CompanyCodeId));
            Attribute(nameof(BuildingDto.CompanyCodeCode));
            Attribute(nameof(BuildingDto.CompanyCodeName));
            Attribute(nameof(BuildingDto.AdministrationType));
            Attribute(nameof(BuildingDto.ManagementId));
            Attribute(nameof(BuildingDto.ManagementIdNum));
            Attribute(nameof(BuildingDto.EconomicUnitId));
            Attribute(nameof(BuildingDto.EconomicUnitIdNum));
            Attribute(nameof(BuildingDto.EconomicUnitName));
            Attribute(nameof(BuildingDto.BuildingId));
        }
    }
  1. Trying to use it like this
[HttpGet]
        [Route("buildings")]
        [Paginated(PerPage = 25, PageSizeLimit = 100)]
        [ReturnsResource(typeof(BuidlingApiResources))]
        public IHttpActionResult GetBuildings([FromUri] string apiKey)
        {
            var messages = new Messages();
            if (string.IsNullOrWhiteSpace(apiKey))
            {
                messages.AddValidationError("The api key is missing.");
            }
            
            if (messages.HasErrors)
            {
                return ApiControllerTools.BadRequestWithErrors(this, messages);
            }
            
            var result = BtsService.GetBuildings(apiKey, messages);
            
            if (messages.HasErrors)
            {
                return ApiControllerTools.BadRequestWithErrors(this, messages);
            }
            
            return Ok(result);
        }
        

abhalesd avatar Oct 19 '22 14:10 abhalesd

I believe the issue may be that you are returning IHttpActionResult. I don't remember if that is supported or not. But if this is a new project I recommend using .net 6 with ASP core and not 4.x with ASP Web API.

joukevandermaas avatar Oct 19 '22 14:10 joukevandermaas