/// <summary>
        /// Generates a URL with an absolute path for the specified <paramref name="pageName"/>. See the remarks section for
        /// important security information.
        /// </summary>
        /// <param name="urlHelper">The <see cref="IUrlHelper"/>.</param>
        /// <param name="pageName">The page name to generate the url for.</param>
        /// <param name="pageHandler">The handler to generate the url for.</param>
        /// <param name="values">An object that contains route values.</param>
        /// <param name="protocol">The protocol for the URL, such as "http" or "https".</param>
        /// <param name="host">The host name for the URL.</param>
        /// <param name="fragment">The fragment for the URL.</param>
        /// <returns>The generated URL.</returns>
        /// <remarks>
        /// <para>
        /// The value of <paramref name="host"/> should be a trusted value. Relying on the value of the current request
        /// can allow untrusted input to influence the resulting URI unless the <c>Host</c> header has been validated.
        /// See the deployment documentation for instructions on how to properly validate the <c>Host</c> header in
        /// your deployment environment.
        /// </para>
        /// </remarks>
        public static string Page(
            this IUrlHelper urlHelper,
            string pageName,
            string pageHandler,
            object values,
            string protocol,
            string host,
            string fragment)
        {
            if (urlHelper == null)
            {
                throw new ArgumentNullException(nameof(urlHelper));
            }

            var routeValues   = new RouteValueDictionary(values);
            var ambientValues = urlHelper.ActionContext.RouteData.Values;

            UrlHelperBase.NormalizeRouteValuesForPage(urlHelper.ActionContext, pageName, pageHandler, routeValues, ambientValues);

            return(urlHelper.RouteUrl(
                       routeName: null,
                       values: routeValues,
                       protocol: protocol,
                       host: host,
                       fragment: fragment));
        }
        private static RouteValuesAddress CreateAddress(HttpContext httpContext, string action, string controller, object values)
        {
            var explicitValues = new RouteValueDictionary(values);
            var ambientValues  = GetAmbientValues(httpContext);

            UrlHelperBase.NormalizeRouteValuesForAction(action, controller, explicitValues, ambientValues);

            return(new RouteValuesAddress()
            {
                AmbientValues = ambientValues,
                ExplicitValues = explicitValues
            });
        }
        private static RouteValuesAddress CreateAddress(HttpContext httpContext, string page, string handler, object values)
        {
            var explicitValues = new RouteValueDictionary(values);
            var ambientValues  = GetAmbientValues(httpContext);

            UrlHelperBase.NormalizeRouteValuesForPage(context: null, page, handler, explicitValues, ambientValues);

            return(new RouteValuesAddress()
            {
                AmbientValues = ambientValues,
                ExplicitValues = explicitValues
            });
        }
Exemple #4
0
    public void AppendPathAndFragment_HandlesLeadingAndTrailingSlashes(
        string appBase,
        string virtualPath,
        string expected)
    {
        // Arrange
        var services    = CreateServices();
        var httpContext = CreateHttpContext(services, appBase, host: null, protocol: null);
        var builder     = new StringBuilder();

        // Act
        UrlHelperBase.AppendPathAndFragment(builder, httpContext.Request.PathBase, virtualPath, string.Empty);

        // Assert
        Assert.Equal(expected, builder.ToString());
    }
Exemple #5
0
    public void AppendPathAndFragment_AppendsFragments(
        string appBase,
        string virtualPath,
        string expected)
    {
        // Arrange
        var fragmentValue = "fragment-value";

        expected += $"#{fragmentValue}";
        var services    = CreateServices();
        var httpContext = CreateHttpContext(services, appBase, host: null, protocol: null);
        var builder     = new StringBuilder();

        // Act
        UrlHelperBase.AppendPathAndFragment(builder, httpContext.Request.PathBase, virtualPath, fragmentValue);

        // Assert
        Assert.Equal(expected, builder.ToString());
    }