当前位置: 首页>>代码示例>>C#>>正文


C# TagHelpers.TagHelperExecutionContext类代码示例

本文整理汇总了C#中Microsoft.AspNet.Razor.Runtime.TagHelpers.TagHelperExecutionContext的典型用法代码示例。如果您正苦于以下问题:C# TagHelperExecutionContext类的具体用法?C# TagHelperExecutionContext怎么用?C# TagHelperExecutionContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


TagHelperExecutionContext类属于Microsoft.AspNet.Razor.Runtime.TagHelpers命名空间,在下文中一共展示了TagHelperExecutionContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: RunAsync_CallsInitPriorToProcessAsync

        public async Task RunAsync_CallsInitPriorToProcessAsync()
        {
            // Arrange
            var runner = new TagHelperRunner();
            var executionContext = new TagHelperExecutionContext("p", TagMode.StartTagAndEndTag);
            var incrementer = 0;
            var callbackTagHelper = new CallbackTagHelper(
                initCallback: () =>
                {
                    Assert.Equal(0, incrementer);

                    incrementer++;
                },
                processAsyncCallback: () =>
                {
                    Assert.Equal(1, incrementer);

                    incrementer++;
                });
            executionContext.Add(callbackTagHelper);

            // Act
            await runner.RunAsync(executionContext);

            // Assert
            Assert.Equal(2, incrementer);
        }
开发者ID:huoxudong125,项目名称:Razor,代码行数:27,代码来源:TagHelperRunnerTest.cs

示例2: RunAsync

        /// <summary>
        /// Calls the <see cref="ITagHelper.ProcessAsync"/> method on <see cref="ITagHelper"/>s.
        /// </summary>
        /// <param name="executionContext">Contains information associated with running <see cref="ITagHelper"/>s.
        /// </param>
        /// <returns>Resulting <see cref="TagHelperOutput"/> from processing all of the
        /// <paramref name="executionContext"/>'s <see cref="ITagHelper"/>s.</returns>
        public async Task<TagHelperOutput> RunAsync(TagHelperExecutionContext executionContext)
        {
            if (executionContext == null)
            {
                throw new ArgumentNullException(nameof(executionContext));
            }

            var tagHelperContext = new TagHelperContext(
                executionContext.AllAttributes,
                executionContext.Items,
                executionContext.UniqueId,
                executionContext.GetChildContentAsync);
            var tagHelperOutput = new TagHelperOutput(
                executionContext.TagName,
                executionContext.HTMLAttributes)
            {
                TagMode = executionContext.TagMode,
            };
            var orderedTagHelpers = executionContext.TagHelpers.OrderBy(tagHelper => tagHelper.Order);

            foreach (var tagHelper in orderedTagHelpers)
            {
                await tagHelper.ProcessAsync(tagHelperContext, tagHelperOutput);
            }

            return tagHelperOutput;
        }
开发者ID:rahulchrty,项目名称:Razor,代码行数:34,代码来源:TagHelperRunner.cs

示例3: Begin

        /// <summary>
        /// Starts a <see cref="TagHelperExecutionContext"/> scope.
        /// </summary>
        /// <param name="tagName">The HTML tag name that the scope is associated with.</param>
        /// <param name="selfClosing">
        /// <see cref="bool"/> indicating whether or not the tag of this scope is self-closing.
        /// </param>
        /// <param name="uniqueId">An identifier unique to the HTML element this scope is for.</param>
        /// <param name="executeChildContentAsync">A delegate used to execute the child content asynchronously.</param>
        /// <param name="startTagHelperWritingScope">A delegate used to start a writing scope in a Razor page.</param>
        /// <param name="endTagHelperWritingScope">A delegate used to end a writing scope in a Razor page.</param>
        /// <returns>A <see cref="TagHelperExecutionContext"/> to use.</returns>
        public TagHelperExecutionContext Begin(
            [NotNull] string tagName,
            bool selfClosing,
            [NotNull] string uniqueId,
            [NotNull] Func<Task> executeChildContentAsync,
            [NotNull] Action startTagHelperWritingScope,
            [NotNull] Func<TagHelperContent> endTagHelperWritingScope)
        {
            IDictionary<object, object> items;

            // If we're not wrapped by another TagHelper, then there will not be a parentExecutionContext.
            if (_executionScopes.Count > 0)
            {
                items = new CopyOnWriteDictionary<object, object>(
                    _executionScopes.Peek().Items,
                    comparer: EqualityComparer<object>.Default);
            }
            else
            {
                items = new Dictionary<object, object>();
            }

            var executionContext = new TagHelperExecutionContext(
                tagName,
                selfClosing,
                items,
                uniqueId,
                executeChildContentAsync,
                startTagHelperWritingScope,
                endTagHelperWritingScope);

            _executionScopes.Push(executionContext);

            return executionContext;
        }
开发者ID:rohitpoudel,项目名称:Razor,代码行数:47,代码来源:TagHelperScopeManager.cs

示例4: GetChildContentAsync_CachesValue

        public async Task GetChildContentAsync_CachesValue()
        {
            // Arrange
            var defaultTagHelperContent = new DefaultTagHelperContent();
            var expectedContent = string.Empty;
            var executionContext = new TagHelperExecutionContext(
                "p",
                selfClosing: false,
                items: null,
                uniqueId: string.Empty,
                executeChildContentAsync: () =>
                {
                    if (string.IsNullOrEmpty(expectedContent))
                    {
                        expectedContent = "Hello from child content: " + Guid.NewGuid().ToString();
                    }

                    defaultTagHelperContent.SetContent(expectedContent);

                    return Task.FromResult(result: true);
                },
                startTagHelperWritingScope: () => { },
                endTagHelperWritingScope: () => defaultTagHelperContent);

            // Act
            var content1 = await executionContext.GetChildContentAsync();
            var content2 = await executionContext.GetChildContentAsync();

            // Assert
            Assert.Equal(expectedContent, content1.GetContent());
            Assert.Equal(expectedContent, content2.GetContent());
        }
开发者ID:billwaddyjr,项目名称:Razor,代码行数:32,代码来源:TagHelperExecutionContextTest.cs

示例5: RunAsync

        /// <summary>
        /// Calls the <see cref="ITagHelper.ProcessAsync"/> method on <see cref="ITagHelper"/>s.
        /// </summary>
        /// <param name="executionContext">Contains information associated with running <see cref="ITagHelper"/>s.
        /// </param>
        /// <returns>Resulting <see cref="TagHelperOutput"/> from processing all of the
        /// <paramref name="executionContext"/>'s <see cref="ITagHelper"/>s.</returns>
        public async Task<TagHelperOutput> RunAsync(TagHelperExecutionContext executionContext)
        {
            if (executionContext == null)
            {
                throw new ArgumentNullException(nameof(executionContext));
            }

            var tagHelperContext = new TagHelperContext(
                executionContext.AllAttributes,
                executionContext.Items,
                executionContext.UniqueId);

            OrderTagHelpers(executionContext.TagHelpers);

            for (var i = 0; i < executionContext.TagHelpers.Count; i++)
            {
                executionContext.TagHelpers[i].Init(tagHelperContext);
            }

            var tagHelperOutput = new TagHelperOutput(
                executionContext.TagName,
                executionContext.HTMLAttributes,
                executionContext.GetChildContentAsync)
            {
                TagMode = executionContext.TagMode,
            };

            for (var i = 0; i < executionContext.TagHelpers.Count; i++)
            {
                await executionContext.TagHelpers[i].ProcessAsync(tagHelperContext, tagHelperOutput);
            }

            return tagHelperOutput;
        }
开发者ID:leloulight,项目名称:Razor,代码行数:41,代码来源:TagHelperRunner.cs

示例6: GetChildContentAsync_CachesValue

        public async Task GetChildContentAsync_CachesValue()
        {
            // Arrange
            var defaultTagHelperContent = new DefaultTagHelperContent();
            var content = string.Empty;
            var expectedContent = string.Empty;
            var executionContext = new TagHelperExecutionContext(
                "p",
                tagMode: TagMode.StartTagAndEndTag,
                items: new Dictionary<object, object>(),
                uniqueId: string.Empty,
                executeChildContentAsync: () =>
                {
                    if (string.IsNullOrEmpty(expectedContent))
                    {
                        content = "Hello from child content: " + Guid.NewGuid().ToString();
                        expectedContent = $"HtmlEncode[[{content}]]";
                    }

                    defaultTagHelperContent.SetContent(content);

                    return Task.FromResult(result: true);
                },
                startTagHelperWritingScope: () => { },
                endTagHelperWritingScope: () => defaultTagHelperContent);

            // Act
            var content1 = await executionContext.GetChildContentAsync(useCachedResult: true);
            var content2 = await executionContext.GetChildContentAsync(useCachedResult: true);

            // Assert
            Assert.Equal(expectedContent, content1.GetContent(new HtmlTestEncoder()));
            Assert.Equal(expectedContent, content2.GetContent(new HtmlTestEncoder()));
        }
开发者ID:leloulight,项目名称:Razor,代码行数:34,代码来源:TagHelperExecutionContextTest.cs

示例7: SelfClosing_ReturnsTrueOrFalseAsExpected

        public void SelfClosing_ReturnsTrueOrFalseAsExpected(bool selfClosing)
        {
            // Arrange & Act
            var executionContext = new TagHelperExecutionContext("p", selfClosing);

            // Assert 
            Assert.Equal(selfClosing, executionContext.SelfClosing);
        }
开发者ID:billwaddyjr,项目名称:Razor,代码行数:8,代码来源:TagHelperExecutionContextTest.cs

示例8: TagMode_ReturnsExpectedValue

        public void TagMode_ReturnsExpectedValue(TagMode tagMode)
        {
            // Arrange & Act
            var executionContext = new TagHelperExecutionContext("p", tagMode);

            // Assert
            Assert.Equal(tagMode, executionContext.TagMode);
        }
开发者ID:leloulight,项目名称:Razor,代码行数:8,代码来源:TagHelperExecutionContextTest.cs

示例9: ParentItems_SetsItemsProperty

        public void ParentItems_SetsItemsProperty()
        {
            // Arrange
            var expectedItems = new Dictionary<object, object>
            {
                { "test-entry", 1234 }
            };

            // Act
            var executionContext = new TagHelperExecutionContext(
                "p",
                selfClosing: false,
                items: expectedItems,
                uniqueId: string.Empty,
                executeChildContentAsync: async () => await Task.FromResult(result: true),
                startTagHelperWritingScope: () => { },
                endTagHelperWritingScope: () => new DefaultTagHelperContent());

            // Assert
            Assert.NotNull(executionContext.Items);
            Assert.Same(expectedItems, executionContext.Items);
        }
开发者ID:billwaddyjr,项目名称:Razor,代码行数:22,代码来源:TagHelperExecutionContextTest.cs

示例10: RunAsync_OrdersTagHelpers

        public async Task RunAsync_OrdersTagHelpers(
            int[] tagHelperOrders,
            int[] expectedTagHelperOrders)
        {
            // Arrange
            var runner = new TagHelperRunner();
            var executionContext = new TagHelperExecutionContext("p", TagMode.StartTagAndEndTag);
            var processOrder = new List<int>();

            foreach (var order in tagHelperOrders)
            {
                var orderedTagHelper = new OrderedTagHelper(order)
                {
                    ProcessOrderTracker = processOrder
                };
                executionContext.Add(orderedTagHelper);
            }

            // Act
            await runner.RunAsync(executionContext);

            // Assert
            Assert.Equal(expectedTagHelperOrders, processOrder);
        }
开发者ID:rahulchrty,项目名称:Razor,代码行数:24,代码来源:TagHelperRunnerTest.cs

示例11: Add_MaintainsMultipleTagHelpers

        public void Add_MaintainsMultipleTagHelpers()
        {
            // Arrange
            var executionContext = new TagHelperExecutionContext("p", selfClosing: false);
            var tagHelper1 = new PTagHelper();
            var tagHelper2 = new PTagHelper();

            // Act
            executionContext.Add(tagHelper1);
            executionContext.Add(tagHelper2);

            // Assert
            var tagHelpers = executionContext.TagHelpers.ToArray();
            Assert.Equal(2, tagHelpers.Length);
            Assert.Same(tagHelper1, tagHelpers[0]);
            Assert.Same(tagHelper2, tagHelpers[1]);

        }
开发者ID:billwaddyjr,项目名称:Razor,代码行数:18,代码来源:TagHelperExecutionContextTest.cs

示例12: Add_MaintainsTagHelpers

        public void Add_MaintainsTagHelpers()
        {
            // Arrange
            var executionContext = new TagHelperExecutionContext("p", selfClosing: false);
            var tagHelper = new PTagHelper();

            // Act
            executionContext.Add(tagHelper);

            // Assert
            var singleTagHelper = Assert.Single(executionContext.TagHelpers);
            Assert.Same(tagHelper, singleTagHelper);
        }
开发者ID:billwaddyjr,项目名称:Razor,代码行数:13,代码来源:TagHelperExecutionContextTest.cs

示例13: TagHelperExecutionContext_MaintainsAllAttributes

        public void TagHelperExecutionContext_MaintainsAllAttributes()
        {
            // Arrange
            var executionContext = new TagHelperExecutionContext("p", selfClosing: false);
            var expectedAttributes = new Dictionary<string, object>
            {
                { "class", "btn" },
                { "something", true },
                { "foo", "bar" }
            };

            // Act
            executionContext.AddHtmlAttribute("class", "btn");
            executionContext.AddTagHelperAttribute("something", true);
            executionContext.AddHtmlAttribute("foo", "bar");

            // Assert
            Assert.Equal(expectedAttributes, executionContext.AllAttributes);
        }
开发者ID:billwaddyjr,项目名称:Razor,代码行数:19,代码来源:TagHelperExecutionContextTest.cs

示例14: AddMinimizedHtmlAttribute_MaintainsHTMLAttributes_SomeMinimized

        public void AddMinimizedHtmlAttribute_MaintainsHTMLAttributes_SomeMinimized()
        {
            // Arrange
            var executionContext = new TagHelperExecutionContext("input", tagMode: TagMode.SelfClosing);
            var expectedAttributes = new TagHelperAttributeList
            {
                { "class", "btn" },
                { "foo", "bar" }
            };
            expectedAttributes.Add(new TagHelperAttribute { Name = "checked", Minimized = true });
            expectedAttributes.Add(new TagHelperAttribute { Name = "visible", Minimized = true });

            // Act
            executionContext.AddHtmlAttribute("class", "btn");
            executionContext.AddHtmlAttribute("foo", "bar");
            executionContext.AddMinimizedHtmlAttribute("checked");
            executionContext.AddMinimizedHtmlAttribute("visible");

            // Assert
            Assert.Equal(
                expectedAttributes,
                executionContext.HTMLAttributes,
                CaseSensitiveTagHelperAttributeComparer.Default);
        }
开发者ID:leloulight,项目名称:Razor,代码行数:24,代码来源:TagHelperExecutionContextTest.cs

示例15: WriteTagHelperAsync

        #pragma warning disable 1998
        public override async Task ExecuteAsync()
        {
            __tagHelperRunner = __tagHelperRunner ?? new TagHelperRunner();
            Instrumentation.BeginContext(33, 2, true);
            WriteLiteral("\r\n");
            Instrumentation.EndContext();
            __tagHelperExecutionContext = __tagHelperScopeManager.Begin("input", true, "test", async() => {
            }
            , StartTagHelperWritingScope, EndTagHelperWritingScope);
            __InputTagHelper = CreateTagHelper<InputTagHelper>();
            __tagHelperExecutionContext.Add(__InputTagHelper);
            __InputTagHelper.Type = "checkbox";
            __tagHelperExecutionContext.AddTagHelperAttribute("type", __InputTagHelper.Type);
#line 3 "DuplicateTargetTagHelper.cshtml"
     __InputTagHelper.Checked = true;

#line default
#line hidden
            __tagHelperExecutionContext.AddTagHelperAttribute("checked", __InputTagHelper.Checked);
            __CatchAllTagHelper = CreateTagHelper<CatchAllTagHelper>();
            __tagHelperExecutionContext.Add(__CatchAllTagHelper);
            __CatchAllTagHelper.Type = __InputTagHelper.Type;
            __CatchAllTagHelper.Checked = __InputTagHelper.Checked;
            __tagHelperExecutionContext.Output = await __tagHelperRunner.RunAsync(__tagHelperExecutionContext);
            await WriteTagHelperAsync(__tagHelperExecutionContext);
            __tagHelperExecutionContext = __tagHelperScopeManager.End();
        }
开发者ID:rohitpoudel,项目名称:Razor,代码行数:28,代码来源:DuplicateTargetTagHelper.cs


注:本文中的Microsoft.AspNet.Razor.Runtime.TagHelpers.TagHelperExecutionContext类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。