Could not find an IRouter associated with the ActionContext

Viewed 742

I'm trying to implement pagination for my ASP.NET Core 3.0 web-api but I'm getting this error:

System.InvalidOperationException: Could not find an IRouter associated with the ActionContext. If your application is using endpoint routing then you can get a IUrlHelperFactory with dependency injection and use it to create a UrlHelper, or use Microsoft.AspNetCore.Routing.LinkGenerator.

Just to clarify, I'm not using the repository pattern (for study purposes)

Here's my get method:

[HttpGet]
public PagedList<Record> GetRecords(RecordsResourceParameters recordsResourceParameters)
{
    IQueryable<Record> query = _context.Set<Record>();
    PagedList<Record> records = PagedList<Record>.Create(query,
        recordsResourceParameters.PageNumber,
        recordsResourceParameters.PageSize);

    var previousPageLink = records.HasPrevious ? 
        CreateResourceUri(recordsResourceParameters, ResourceUriType.Previous): null; 

    var nextPageLink = records.HasNext ? 
        CreateResourceUri(recordsResourceParameters, ResourceUriType.Next): null; 

    var paginationMetadata  = new 
    {
        totalCount = records.TotalCount,
        pageSize = records.PageSize,
        currentPage = records.CurrentPage,
        totalPages = records.TotalPages,
        previousPageLink = previousPageLink,
        nextPageLink = nextPageLink
    }; 

    Response.Headers.Add("X-Pagination", 
        Newtonsoft.Json.JsonConvert.SerializeObject(paginationMetadata));

        return records; 
}

private string CreateResourceUri(RecordsResourceParameters recordsResourceParameters, ResourceUriType type)
{
    switch (type)
    {
        case ResourceUriType.Previous:
            return _urlhelper.Link("GetRecords",
            new
            {
                pageNumber = recordsResourceParameters.PageNumber - 1,
                pageSize = recordsResourceParameters.PageSize
            });

        case ResourceUriType.Next:
            return _urlhelper.Link("GetRecords",
            new
            {
                pageNumber = recordsResourceParameters.PageNumber + 1,
                pageSize = recordsResourceParameters.PageSize
            });

        default:
            return _urlhelper.Link("GetRecords",
            new
            {
                pageNumber = recordsResourceParameters.PageNumber - 1,
                pageSize = recordsResourceParameters.PageSize
            });
    }
}

Here's startup:

public void ConfigureServices(IServiceCollection services)
{

    // Added Newtonsoft to fix error: Json couldn't be converted into int32
    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
    services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();

    services.AddScoped<IUrlHelper, UrlHelper>(x => { 
        var actionContext = x.GetService<IActionContextAccessor>().ActionContext;
        return new UrlHelper(actionContext);
        });

    services.AddControllers().AddNewtonsoftJson();

    services.AddDbContext<DataContext>(options =>
    {
        options.UseSqlServer(
            @"Server=localhost;Database=db.Personaltracker;User ID=xx;Password=xxx;");
    }); 
0 Answers
Related