Exemplo n.º 1
0
        /// <inheritdoc />
        /// <remarks>
        /// Does nothing if user provides an <c>action</c> attribute and <see cref="AntiForgery"/> is <c>null</c> or
        /// <c>false</c>.
        /// </remarks>
        /// <exception cref="InvalidOperationException">
        /// Thrown if <c>action</c> attribute is provided and <see cref="Action"/> or <see cref="Controller"/> are
        /// non-<c>null</c> or if the user provided <c>asp-route-*</c> attributes.
        /// </exception>
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            var antiForgeryDefault = true;
            var routePrefixedAttributes = output.FindPrefixedAttributes(RouteAttributePrefix);

            // If "action" is already set, it means the user is attempting to use a normal <form>.
            if (output.Attributes.ContainsKey(HtmlActionAttributeName))
            {
                if (Action != null || Controller != null || routePrefixedAttributes.Any())
                {
                    // User also specified bound attributes we cannot use.
                    throw new InvalidOperationException(
                        Resources.FormatFormTagHelper_CannotOverrideAction(
                            "<form>",
                            HtmlActionAttributeName,
                            ActionAttributeName,
                            ControllerAttributeName,
                            RouteAttributePrefix));
                }

                // User is using the FormTagHelper like a normal <form> tag. Anti-forgery default should be false to
                // not force the anti-forgery token on the user.
                antiForgeryDefault = false;
            }
            else
            {
                var routeValues = GetRouteValues(output, routePrefixedAttributes);
                var tagBuilder = Generator.GenerateForm(ViewContext,
                                                        Action,
                                                        Controller,
                                                        routeValues,
                                                        method: null,
                                                        htmlAttributes: null);

                if (tagBuilder != null)
                {
                    output.MergeAttributes(tagBuilder);
                    output.PostContent.Append(tagBuilder.InnerHtml);
                }
            }

            if (AntiForgery ?? antiForgeryDefault)
            {
                var antiForgeryTagBuilder = Generator.GenerateAntiForgery(ViewContext);
                if (antiForgeryTagBuilder != null)
                {
                    output.PostContent.Append(antiForgeryTagBuilder.ToString(TagRenderMode.SelfClosing));
                }
            }
        }
Exemplo n.º 2
0
        /// <inheritdoc />
        /// <remarks>Does nothing if user provides an <c>href</c> attribute.</remarks>
        /// <exception cref="InvalidOperationException">
        /// Thrown if <c>href</c> attribute is provided and <see cref="Action"/>, <see cref="Controller"/>,
        /// <see cref="Fragment"/>, <see cref="Host"/>, <see cref="Protocol"/>, or <see cref="Route"/> are
        /// non-<c>null</c> or if the user provided <c>asp-route-*</c> attributes. Also thrown if <see cref="Route"/>
        /// and one or both of <see cref="Action"/> and <see cref="Controller"/> are non-<c>null</c>.
        /// </exception>
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            var routePrefixedAttributes = output.FindPrefixedAttributes(RouteAttributePrefix);

            // If "href" is already set, it means the user is attempting to use a normal anchor.
            if (output.Attributes.ContainsKey(Href))
            {
                if (Action != null ||
                    Controller != null ||
                    Route != null ||
                    Protocol != null ||
                    Host != null ||
                    Fragment != null ||
                    routePrefixedAttributes.Any())
                {
                    // User specified an href and one of the bound attributes; can't determine the href attribute.
                    throw new InvalidOperationException(
                        Resources.FormatAnchorTagHelper_CannotOverrideHref(
                            "<a>",
                            ActionAttributeName,
                            ControllerAttributeName,
                            RouteAttributeName,
                            ProtocolAttributeName,
                            HostAttributeName,
                            FragmentAttributeName,
                            RouteAttributePrefix,
                            Href));
                }
            }
            else
            {
                TagBuilder tagBuilder;
                var routeValues = GetRouteValues(output, routePrefixedAttributes);

                if (Route == null)
                {
                    tagBuilder = Generator.GenerateActionLink(linkText: string.Empty,
                                                              actionName: Action,
                                                              controllerName: Controller,
                                                              protocol: Protocol,
                                                              hostname: Host,
                                                              fragment: Fragment,
                                                              routeValues: routeValues,
                                                              htmlAttributes: null);
                }
                else if (Action != null || Controller != null)
                {
                    // Route and Action or Controller were specified. Can't determine the href attribute.
                    throw new InvalidOperationException(
                        Resources.FormatAnchorTagHelper_CannotDetermineHrefRouteActionOrControllerSpecified(
                            "<a>",
                            RouteAttributeName,
                            ActionAttributeName,
                            ControllerAttributeName,
                            Href));
                }
                else
                {
                    tagBuilder = Generator.GenerateRouteLink(linkText: string.Empty,
                                                             routeName: Route,
                                                             protocol: Protocol,
                                                             hostName: Host,
                                                             fragment: Fragment,
                                                             routeValues: routeValues,
                                                             htmlAttributes: null);
                }

                if (tagBuilder != null)
                {
                    output.MergeAttributes(tagBuilder);
                }
            }
        }
        public void FindPrefixedAttributes_ReturnsEmpty_AttributeListIfNoAttributesPrefixed()
        {
            // Arrange
            var tagHelperOutput = new TagHelperOutput(
                "p",
                attributes: new Dictionary<string, object>()
                {
                    { "routeHello", "World" },
                    { "Routee-I", "Am" }
                });

            // Act
            var attributes = tagHelperOutput.FindPrefixedAttributes("route-");

            // Assert
            Assert.Empty(attributes);
            var attribute = Assert.Single(tagHelperOutput.Attributes, kvp => kvp.Key.Equals("routeHello"));
            Assert.Equal(attribute.Value, "World");
            attribute = Assert.Single(tagHelperOutput.Attributes, kvp => kvp.Key.Equals("Routee-I"));
            Assert.Equal(attribute.Value, "Am");
        }
        public void RemoveRange_RemovesProvidedAttributes()
        {
            // Arrange
            var tagHelperOutput = new TagHelperOutput(
                "p",
                attributes: new Dictionary<string, object>()
                {
                    { "route-Hello", "World" },
                    { "Route-I", "Am" }
                });
            var expectedAttribute = new KeyValuePair<string, object>("type", "btn");
            tagHelperOutput.Attributes.Add(expectedAttribute);
            var attributes = tagHelperOutput.FindPrefixedAttributes("route-");

            // Act
            tagHelperOutput.RemoveRange(attributes);

            // Assert
            var attribute = Assert.Single(tagHelperOutput.Attributes);
            Assert.Equal(expectedAttribute, attribute);
        }
Exemplo n.º 5
0
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            RouteValues = output.TrimPrefixedAttributes(RouteAttributePrefix);
            AjaxValues = output.FindPrefixedAttributes(AjaxAttributePrefix);

            ApplyActionAttributes();
            ApplyPaginationAttributes(context);
            ApplyIconTextAttributes();

            output.TagName = null;
            //if there are no rows then don't show
            if (PageSize != 0 && Total != 0)
                output.Content.SetContent(Create(PageIndex, Total, PageSize));

            await base.ProcessAsync(context, output);
        }