Wednesday, April 15, 2020

Upgrade to .NET Core 3.1 - 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

The error itself exposes the solution. You need to stop using: 

            return new UrlHelper(helper.ViewContext).DisplayLink(action, linkText, returnLink);

And instead use (notice I'm not using DI, but you should use DI to retrieve IUrlHelperFactory):

            var urlHelperFactory = ServiceLocator.Current.GetInstance<IUrlHelperFactory>();
            return urlHelperFactory.GetUrlHelper(helper.ViewContext).DisplayLink(action, linkText, returnLink);

No comments: