public static Uri GetBaseAddress(this HttpRequest request, IUrlHelper?urlHelper = null)
        {
            const string xOriginaldProto = "X-Original-Proto";
            const string xOriginalHost   = "X-Original-Host";

            if (request != null)
            {
                request.Headers.TryGetValue(xOriginaldProto, out var xOriginalProtocolValue);

                if (string.IsNullOrWhiteSpace(xOriginalProtocolValue))
                {
                    xOriginalProtocolValue = request.Scheme ?? Uri.UriSchemeHttp;
                }

                request.Headers.TryGetValue(xOriginalHost, out var xOriginalHostValue);

                if (string.IsNullOrWhiteSpace(xOriginalHostValue))
                {
                    xOriginalHostValue = request.Host.Value;
                }

                return(new Uri($"{xOriginalProtocolValue}://{xOriginalHostValue}{urlHelper?.Content("~")}"));
            }

            return(default);
 private void setUrlHelper(ViewContext viewContext)
 {
     _urlHelper = viewContext.HttpContext.Items.Values.OfType <IUrlHelper>().FirstOrDefault();
     if (_urlHelper == null)
     {
         throw new InvalidOperationException("Failed to find the IUrlHelper of ViewContext.HttpContext.");
     }
 }
        private static IHtmlContent GenerateLink <T>(T model, IUrlHelper?url, String action, String iconClass)
        {
            TagBuilder link = new TagBuilder("a");

            link.Attributes["href"]  = url?.Action(action, RouteFor(model));
            link.Attributes["title"] = Resource.ForAction(action);
            link.AddCssClass(iconClass);

            return(link);
        }
        public static Uri?GetBaseAddress(this HttpRequest request, IUrlHelper?urlHelper = null)
        {
            if (request != null)
            {
                if (request.Headers.TryGetValue("x-forwarded-proto", out var forwardedProtocol) &&
                    request.Headers.TryGetValue("x-original-host", out var originalHost))
                {
                    return(new Uri($"{forwardedProtocol}://{originalHost}"));
                }

                return(string.IsNullOrWhiteSpace(request.Scheme) ? default : new Uri($"{request.Scheme}://{request.Host}{urlHelper?.Content("~")}"));
            }

            return(default);
        public static IGridColumn <T, IHtmlContent> AddAction <T>(this IGridColumnsOf <T> columns, String action, String iconClass) where T : class
        {
            if (!IsAuthorizedFor(columns.Grid.ViewContext !, action))
            {
                return(new GridColumn <T, IHtmlContent>(columns.Grid, model => HtmlString.Empty));
            }

            IUrlHelperFactory?factory = columns.Grid.ViewContext?.HttpContext.RequestServices.GetService <IUrlHelperFactory>();
            IUrlHelper?       url     = factory?.GetUrlHelper(columns.Grid.ViewContext);

            return(columns
                   .Add(model => GenerateLink(model, url, action, iconClass))
                   .Css($"action-cell {action.ToLower()}"));
        }
Пример #6
0
        /// <summary>
        /// Returns a href for the given content path.
        /// </summary>
        /// <param name="contentPath">The content path.</param>
        /// <returns>The href for the contentPath.</returns>
        public virtual string Href(string contentPath)
        {
            if (contentPath == null)
            {
                throw new ArgumentNullException(nameof(contentPath));
            }

            if (_urlHelper == null)
            {
                var viewContext = ViewContext;
                var services    = viewContext.HttpContext.RequestServices;
                var factory     = services.GetRequiredService <IUrlHelperFactory>();
                _urlHelper = factory.GetUrlHelper(viewContext);
            }

            return(_urlHelper.Content(contentPath));
        }
Пример #7
0
 public SitemapBuilder(IUrlHelper?urlHelper)
 {
     _urlHelper = urlHelper;
 }
Пример #8
0
 /// <summary>
 /// 初始化当前标签上下文。
 /// </summary>
 /// <param name="context">当前HTML标签上下文,包含当前HTML相关信息。</param>
 public override void Init(TagHelperContext context)
 {
     _urlHelper = _urlHelperFactory.GetUrlHelper(ViewContext) !;
 }
Пример #9
0
        /// <inheritdoc />
        /// <remarks>Does nothing if user provides an <c>formAction</c> attribute.</remarks>
        /// <exception cref="InvalidOperationException">
        /// Thrown if <c>formAction</c> attribute is provided and <see cref="Action"/>,
        /// <see cref="Controller"/> or <see cref="Fragment"/>  are non-<c>null</c>
        /// or if the user provided <c>asp-route-*</c> attributes.
        /// </exception>
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            // If "FormAction" is already set, it means the user is attempting to use a normal button or input element.
            if (output.Attributes.ContainsName(FormAction))
            {
                if (Action is not null || Controller is not null || Area is not null ||
                    Page is not null || PageHandler is not null || Fragment is not null ||
                    Route is not null ||
                    (RouteValues is not null && RouteValues.Count > 0))
                {
                    // User specified a FormAction and one of the bound attributes
                    // Can't override that FormAction attribute.
                    throw new InvalidOperationException(
                              CannotOverrideFormAction(
                                  FormAction,
                                  output.TagName,
                                  RouteValuesPrefix,
                                  ActionAttributeName,
                                  ControllerAttributeName,
                                  AreaAttributeName,
                                  FragmentAttributeName,
                                  RouteAttributeName,
                                  PageAttributeName,
                                  PageHandlerAttributeName));
                }

                return;
            }

            var routeLink  = Route is not null;
            var actionLink = Controller is not null || Action is not null;
            var pageLink   = Page is not null || PageHandler is not null;

            if ((routeLink && actionLink) || (routeLink && pageLink) || (actionLink && pageLink))
            {
                var message = string.Join(
                    Environment.NewLine,
                    FormatCannotDetermineAttributeFor(FormAction, '<' + output.TagName + '>'),
                    RouteAttributeName,
                    ControllerAttributeName + ", " + ActionAttributeName,
                    PageAttributeName + ", " + PageHandlerAttributeName);
                throw new InvalidOperationException(message);
            }

            RouteValueDictionary?routeValues = null;

            if (RouteValues is not null && RouteValues.Count > 0)
            {
                routeValues = new RouteValueDictionary(RouteValues);
            }

            if (Area is not null)
            {
                if (routeValues is null)
                {
                    routeValues = new RouteValueDictionary();
                }

                // Unconditionally replace any value from asp-route-area.
                routeValues["area"] = Area;
            }

            var        url       = string.Empty;
            IUrlHelper?urlHelper = UrlHelperFactory.GetUrlHelper(ViewContext);

            if (pageLink)
            {
                url = urlHelper.Page(Page, PageHandler, routeValues, protocol: null, host: null, fragment: Fragment);
            }

            else if (routeLink)
            {
                url = urlHelper.RouteUrl(Route, routeValues, protocol: null, host: null, fragment: Fragment);
            }

            else if (actionLink)
            {
                url = urlHelper.Action(Action, Controller, routeValues, null, null, Fragment);
            }

            if (string.IsNullOrEmpty(url) is false)
            {
                output.Attributes.SetAttribute(FormAction, url);
            }

            // Set the button appearance
            output.Attributes.SetAttribute(nameof(Appearance), Appearance.ToString().ToLowerInvariant());
            output.Attributes.SetAttribute("type", "submit");
            output.Attributes.SetAttribute(FormAction, url);
        }