WebApi Plugin: NewsController/List action does not work by default

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
1 year ago
Hi guys,
Great job on the Web Api plugin. I am currently creating a Next.js site using the web API, but I ran into an issue today. When calling the List action in NewsController, there is an error thrown. I tried calling the endpoint from my frontend, but I also tried using the Try it out button in the Swagger doc to make sure I wasnt doing something weird.
The error produced:

`Cannot create an instance of abstract type Nop.Web.Framework.UI.Paging.BasePageableModel. (Parameter 'type')

It is very puzzling to me as from what I see, NewsPagingFilteringModel only inherits from
the abstract class BasePageableModel, so it definitely should be able to be instantiated.
I looked in the code and I found this:

        public virtual async Task<IActionResult> List([FromBody] BasePageableModelDto command)
        {
            if (!_newsSettings.Enabled)
                return BadRequest($"The setting {nameof(_newsSettings.Enabled)} is not enabled");

            var model = await _newsModelFactory.PrepareNewsItemListModelAsync(command.FromDto<NewsPagingFilteringModel>());
            
            return Ok(model.ToDto<NewsItemListModelDto>());
        }


It seems there wasnt a dto mapping beteween BasePageableDto and NewsPagingFilteringModel, so I added one in the WebApiFrontendMapperConfiguration, but the problem was still there.

So, I went ahead and replaced:

        public virtual async Task<IActionResult> List([FromBody] BasePageableModelDto command)


To my own model (which I also added a mapping for):

        public virtual async Task<IActionResult> List([FromBody] NewsPagingFilteringModelDto command)


But still, the same error is thrown.

Any ideas?
1 year ago
You are right, this method really does not work, and the problem is not so much in the absence of mapping, but in the fact that an abstract class is involved in the mapping. I found a solution to the problem and we will include it in the next release. For you, the algorithm should be like this:

1. add a new model

public class NewsPagingFilteringModelDto : BasePageableModelDto
{
}


2. add it to the mapping
CreateDtoMap<NewsPagingFilteringModel, NewsPagingFilteringModelDto>();


3. change the method definition to
public virtual async Task<IActionResult> List([FromBody] NewsPagingFilteringModelDto command)
1 year ago
Sergei-k wrote:
/* solution */


Thanks so much for the help, that did indeed work!
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.