1

我正在尝试在我的 webapi 的 .net 核心中使用 IUrlhelper 创建一个分页链接。

我得到错误

“值不能为空。参数名称:uriString”

我在启动时有以下代码:

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

然后我有一个类,我建立我的分页链接

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 }));
    }

如果执行以下操作,我的链接始终为空。不要明白!

//注意urlhelper在注入时不为null

        var link = urlHelper.Link("getsomething", new { PageNumber = 1, PageSize = 20});

    Any ideas what I am doing wrong?

    thanks
4

1 回答 1

0

找到了我自己的答案。当我建立链接时,路线的名称必须与路线名称匹配。

于 2018-05-05T06:15:28.167 回答