joukevandermaas / saule

JSON API library for ASP.Net Web API 2.
https://joukevandermaas.github.io/saule
MIT License
76 stars 37 forks source link

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

Open abhalesd opened 2 years ago

abhalesd commented 2 years ago

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?

joukevandermaas commented 2 years ago

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

abhalesd commented 2 years ago

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));
        }
    }
  3. 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);
        }
joukevandermaas commented 2 years ago

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.