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


C# ServiceCollection.AddWebEncoders方法代码示例

本文整理汇总了C#中ServiceCollection.AddWebEncoders方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceCollection.AddWebEncoders方法的具体用法?C# ServiceCollection.AddWebEncoders怎么用?C# ServiceCollection.AddWebEncoders使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ServiceCollection的用法示例。


在下文中一共展示了ServiceCollection.AddWebEncoders方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: AddWebEncoders_DoesNotOverrideExistingRegisteredEncoders

        public void AddWebEncoders_DoesNotOverrideExistingRegisteredEncoders()
        {
            // Arrange
            var serviceCollection = new ServiceCollection();

            // Act
            serviceCollection.AddSingleton<IHtmlEncoder, CommonTestEncoder>();
            serviceCollection.AddSingleton<IJavaScriptStringEncoder, CommonTestEncoder>();
            // we don't register an existing URL encoder
            serviceCollection.AddWebEncoders(options =>
            {
                options.CodePointFilter = new CodePointFilter().AllowChars("ace"); // only these three chars are allowed
            });

            // Assert
            var serviceProvider = serviceCollection.BuildServiceProvider();

            var htmlEncoder = serviceProvider.GetHtmlEncoder();
            Assert.Equal("HtmlEncode[[abcde]]", htmlEncoder.HtmlEncode("abcde"));

            var javaScriptStringEncoder = serviceProvider.GetJavaScriptStringEncoder();
            Assert.Equal("JavaScriptStringEncode[[abcde]]", javaScriptStringEncoder.JavaScriptStringEncode("abcde"));

            var urlEncoder = serviceProvider.GetUrlEncoder();
            Assert.Equal("a%62c%64e", urlEncoder.UrlEncode("abcde"));
        }
开发者ID:TwoToneBytes,项目名称:HttpAbstractions,代码行数:26,代码来源:EncoderServiceCollectionExtensionsTests.cs

示例2: AddWebEncoders_WithOptions_RegistersEncodersWithCustomCodeFilter

        public void AddWebEncoders_WithOptions_RegistersEncodersWithCustomCodeFilter()
        {
            // Arrange
            var serviceCollection = new ServiceCollection();

            // Act
            serviceCollection.AddWebEncoders(options =>
            {
                options.CodePointFilter = new CodePointFilter().AllowChars("ace"); // only these three chars are allowed
            });

            // Assert
            var serviceProvider = serviceCollection.BuildServiceProvider();

            var htmlEncoder = serviceProvider.GetRequiredService<IHtmlEncoder>();
            Assert.Equal("a&#x62;c&#x64;e", htmlEncoder.HtmlEncode("abcde"));
            Assert.Same(htmlEncoder, serviceProvider.GetRequiredService<IHtmlEncoder>()); // as singleton instance

            var javaScriptStringEncoder = serviceProvider.GetRequiredService<IJavaScriptStringEncoder>();
            Assert.Equal(@"a\u0062c\u0064e", javaScriptStringEncoder.JavaScriptStringEncode("abcde"));
            Assert.Same(javaScriptStringEncoder, serviceProvider.GetRequiredService<IJavaScriptStringEncoder>()); // as singleton instance

            var urlEncoder = serviceProvider.GetRequiredService<IUrlEncoder>();
            Assert.Equal("a%62c%64e", urlEncoder.UrlEncode("abcde"));
            Assert.Same(urlEncoder, serviceProvider.GetRequiredService<IUrlEncoder>()); // as singleton instance
        }
开发者ID:TwoToneBytes,项目名称:HttpAbstractions,代码行数:26,代码来源:EncoderServiceCollectionExtensionsTests.cs

示例3: AddWebEncoders_WithOptions_RegistersEncodersWithCustomCodeFilter

        public void AddWebEncoders_WithOptions_RegistersEncodersWithCustomCodeFilter()
        {
            // Arrange
            var serviceCollection = new ServiceCollection();

            // Act
            serviceCollection.AddWebEncoders(options =>
            {
                options.TextEncoderSettings = new TextEncoderSettings();
                options.TextEncoderSettings.AllowCharacters("ace".ToCharArray()); // only these three chars are allowed
            });

            // Assert
            var serviceProvider = serviceCollection.BuildServiceProvider();

            var htmlEncoder = serviceProvider.GetRequiredService<HtmlEncoder>();
            Assert.Equal("a&#x62;c&#x64;e", htmlEncoder.Encode("abcde"));
            Assert.Same(htmlEncoder, serviceProvider.GetRequiredService<HtmlEncoder>()); // as singleton instance

            var javaScriptEncoder = serviceProvider.GetRequiredService<JavaScriptEncoder>();
            Assert.Equal(@"a\u0062c\u0064e", javaScriptEncoder.Encode("abcde"));
            Assert.Same(javaScriptEncoder, serviceProvider.GetRequiredService<JavaScriptEncoder>()); // as singleton instance

            var urlEncoder = serviceProvider.GetRequiredService<UrlEncoder>();
            Assert.Equal("a%62c%64e", urlEncoder.Encode("abcde"));
            Assert.Same(urlEncoder, serviceProvider.GetRequiredService<UrlEncoder>()); // as singleton instance
        }
开发者ID:aspnet,项目名称:HtmlAbstractions,代码行数:27,代码来源:EncoderServiceCollectionExtensionsTests.cs

示例4: AddWebEncoders_DoesNotOverrideExistingRegisteredEncoders

        public void AddWebEncoders_DoesNotOverrideExistingRegisteredEncoders()
        {
            // Arrange
            var serviceCollection = new ServiceCollection();

            // Act
            serviceCollection.AddSingleton<HtmlEncoder, HtmlTestEncoder>();
            serviceCollection.AddSingleton<JavaScriptEncoder, JavaScriptTestEncoder>();
            // we don't register an existing URL encoder
            serviceCollection.AddWebEncoders(options =>
            {
                options.TextEncoderSettings = new TextEncoderSettings();
                options.TextEncoderSettings.AllowCharacters("ace".ToCharArray()); // only these three chars are allowed
            });

            // Assert
            var serviceProvider = serviceCollection.BuildServiceProvider();

            var htmlEncoder = serviceProvider.GetRequiredService<HtmlEncoder>();
            Assert.Equal("HtmlEncode[[abcde]]", htmlEncoder.Encode("abcde"));

            var javaScriptEncoder = serviceProvider.GetRequiredService<JavaScriptEncoder>();
            Assert.Equal("JavaScriptEncode[[abcde]]", javaScriptEncoder.Encode("abcde"));

            var urlEncoder = serviceProvider.GetRequiredService<UrlEncoder>();
            Assert.Equal("a%62c%64e", urlEncoder.Encode("abcde"));
        }
开发者ID:aspnet,项目名称:HtmlAbstractions,代码行数:27,代码来源:EncoderServiceCollectionExtensionsTests.cs

示例5: AddWebEncoders_WithoutOptions_RegistersDefaultEncoders

        public void AddWebEncoders_WithoutOptions_RegistersDefaultEncoders()
        {
            // Arrange
            var serviceCollection = new ServiceCollection();

            // Act
            serviceCollection.AddWebEncoders();

            // Assert
            var serviceProvider = serviceCollection.BuildServiceProvider();
            Assert.Same(HtmlEncoder.Default, serviceProvider.GetRequiredService<IHtmlEncoder>()); // default encoder
            Assert.Same(HtmlEncoder.Default, serviceProvider.GetRequiredService<IHtmlEncoder>()); // as singleton instance
            Assert.Same(JavaScriptStringEncoder.Default, serviceProvider.GetRequiredService<IJavaScriptStringEncoder>()); // default encoder
            Assert.Same(JavaScriptStringEncoder.Default, serviceProvider.GetRequiredService<IJavaScriptStringEncoder>()); // as singleton instance
            Assert.Same(UrlEncoder.Default, serviceProvider.GetRequiredService<IUrlEncoder>()); // default encoder
            Assert.Same(UrlEncoder.Default, serviceProvider.GetRequiredService<IUrlEncoder>()); // as singleton instance
        }
开发者ID:TwoToneBytes,项目名称:HttpAbstractions,代码行数:17,代码来源:EncoderServiceCollectionExtensionsTests.cs


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