public void BreadcrumbTrail_removes_Url_for_current_page() { var request = new DefaultHttpContext().Request; request.Scheme = "https"; request.Host = new HostString("www.example.org"); request.PathBase = new PathString(string.Empty); request.Path = new PathString("/example"); request.QueryString = new QueryString(string.Empty); var httpContext = new Mock <HttpContext>(); httpContext.Setup(x => x.Request).Returns(request); var httpContextAccessor = new Mock <IHttpContextAccessor>(); httpContextAccessor.Setup(x => x.HttpContext).Returns(httpContext.Object); var settings = new BreadcrumbSettings { { new BreadcrumbLevel { Name = "level1", Url = new Uri("/", UriKind.Relative) } }, { new BreadcrumbLevel { Name = "level2", Url = new Uri("/example", UriKind.Relative) } } }; var trail = new BreadcrumbTrailFromConfig(Options.Create(settings), httpContextAccessor.Object).BuildTrail(); Assert.Equal(string.Empty, trail["level2"]); }
/// <summary> /// Creates a new instance of <see cref="BreadcrumbTrailFromConfig"/> /// </summary> /// <param name="breadcrumbSettings">The raw breadcrumb data read from a configuration provider</param> /// <param name="httpContextAccessor">A way to get the HttpContext, used to access the URL of the request the breadcrumb trail should be generated for</param> public BreadcrumbTrailFromConfig(IOptions <BreadcrumbSettings> breadcrumbSettings, IHttpContextAccessor httpContextAccessor) { if (httpContextAccessor?.HttpContext?.Request == null) { throw new ArgumentNullException(nameof(httpContextAccessor)); } _breadcrumbSettings = breadcrumbSettings?.Value ?? throw new ArgumentNullException(nameof(breadcrumbSettings)); _requestUrl = new Uri(httpContextAccessor.HttpContext.Request.GetDisplayUrl()); }