I am trying to create a link for pagination using IUrlhelper in .net core in my webapi.
I get the error
"Value cannot be null.Parameter name: uriString"
I have the following code in my start up:
services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();
services.AddScoped<IUrlHelper>(factory =>
{
var actionContext = factory.GetService<IActionContextAccessor>()
.ActionContext;
return new UrlHelper(actionContext);
});
then I have a class that I build my paging links
public class LinkBuilder
{
public LinkBuilder(IUrlHelper urlHelper,string routeName, PagingInfo pagingInfo)
{
var mylink = CreateLink(urlHelper, "GetMovies", pagingInfo.PageNumber, pagingInfo.PageSize);
etc.....
}
private Uri CreateLink(IUrlHelper urlHelper,
string routeName,
int pageNo,
int pageSize)
{
//CRASHES ON NEW URI! "Value cannot be null.
Parameter name: uriString"
return new Uri(
urlHelper.Link(routeName,
new { PageNumber = pageNo, PageSize = pageSize }));
}
also if do the following my link is always null .Dont get it!!
//note urlhelper is not null when injected
var link = urlHelper.Link("getsomething", new { PageNumber = 1, PageSize = 20});
Any ideas what I am doing wrong?
thanks