本文整理汇总了C#中Microsoft.Framework.DependencyInjection.ServiceCollection.InitializeTagHelper方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceCollection.InitializeTagHelper方法的具体用法?C# ServiceCollection.InitializeTagHelper怎么用?C# ServiceCollection.InitializeTagHelper使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Framework.DependencyInjection.ServiceCollection
的用法示例。
在下文中一共展示了ServiceCollection.InitializeTagHelper方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Activate_InitializesTagHelpersWithMultipleInitializers
public void Activate_InitializesTagHelpersWithMultipleInitializers()
{
// Arrange
var services = new ServiceCollection();
services.InitializeTagHelper<TestTagHelper>((h, vc) =>
{
h.Name = "Test 1";
h.Number = 100;
});
services.InitializeTagHelper<TestTagHelper>((h, vc) =>
{
h.Name += ", Test 2";
h.Number += 100;
});
var httpContext = MakeHttpContext(services.BuildServiceProvider());
var viewContext = MakeViewContext(httpContext);
var activator = new DefaultTagHelperActivator();
var helper = new TestTagHelper();
// Act
activator.Activate(helper, viewContext);
// Assert
Assert.Equal("Test 1, Test 2", helper.Name);
Assert.Equal(200, helper.Number);
}
示例2: Activate_InitializesTagHelpers
public void Activate_InitializesTagHelpers(string name, int number)
{
// Arrange
var services = new ServiceCollection();
services.InitializeTagHelper<TestTagHelper>((h, vc) =>
{
h.Name = name;
h.Number = number;
h.ViewDataValue = vc.ViewData["TestData"];
});
var httpContext = MakeHttpContext(services.BuildServiceProvider());
var viewContext = MakeViewContext(httpContext);
var viewDataValue = new object();
viewContext.ViewData.Add("TestData", viewDataValue);
var activator = new DefaultTagHelperActivator();
var helper = new TestTagHelper();
// Act
activator.Activate(helper, viewContext);
// Assert
Assert.Equal(name, helper.Name);
Assert.Equal(number, helper.Number);
Assert.Same(viewDataValue, helper.ViewDataValue);
}
示例3: Activate_InitializesTagHelpersAfterActivatingProperties
public void Activate_InitializesTagHelpersAfterActivatingProperties()
{
// Arrange
var services = new ServiceCollection();
services.InitializeTagHelper<TestTagHelper>((h, _) => h.ViewContext = MakeViewContext(MakeHttpContext()));
var httpContext = MakeHttpContext(services.BuildServiceProvider());
var viewContext = MakeViewContext(httpContext);
var activator = new DefaultTagHelperActivator();
var helper = new TestTagHelper();
// Act
activator.Activate(helper, viewContext);
// Assert
Assert.NotSame(viewContext, helper.ViewContext);
}
示例4: Activate_InitializesTagHelpersWithCorrectInitializers
public void Activate_InitializesTagHelpersWithCorrectInitializers()
{
// Arrange
var services = new ServiceCollection();
services.InitializeTagHelper<TestTagHelper>((h, vc) =>
{
h.Name = "Test 1";
h.Number = 100;
});
services.InitializeTagHelper<AnotherTestTagHelper>((h, vc) =>
{
h.Name = "Test 2";
h.Number = 102;
});
var httpContext = MakeHttpContext(services.BuildServiceProvider());
var viewContext = MakeViewContext(httpContext);
var activator = new DefaultTagHelperActivator();
var testTagHelper = new TestTagHelper();
var anotherTestTagHelper = new AnotherTestTagHelper();
// Act
activator.Activate(testTagHelper, viewContext);
activator.Activate(anotherTestTagHelper, viewContext);
// Assert
Assert.Equal("Test 1", testTagHelper.Name);
Assert.Equal(100, testTagHelper.Number);
Assert.Equal("Test 2", anotherTestTagHelper.Name);
Assert.Equal(102, anotherTestTagHelper.Number);
}
示例5: FormTagHelper_GeneratesExpectedContent
public async Task FormTagHelper_GeneratesExpectedContent(bool? optionsAntiForgery)
{
// Arrange
var newServices = new ServiceCollection();
newServices.InitializeTagHelper<FormTagHelper>((helper, _) => helper.AntiForgery = optionsAntiForgery);
var server = TestHelper.CreateServer(_app, SiteName,
services =>
{
services.Add(newServices);
_configureServices(services);
});
var client = server.CreateClient();
var expectedMediaType = MediaTypeHeaderValue.Parse("text/html; charset=utf-8");
var outputFile = string.Format(
"compiler/resources/HtmlGenerationWebSite.HtmlGeneration_Home.Form.Options.AntiForgery.{0}.html",
optionsAntiForgery?.ToString() ?? "null");
var expectedContent =
await ResourceFile.ReadResourceAsync(_resourcesAssembly, outputFile, sourceFile: false);
// Act
// The host is not important as everything runs in memory and tests are isolated from each other.
var response = await client.GetAsync("http://localhost/HtmlGeneration_Home/Form");
var responseContent = await response.Content.ReadAsStringAsync();
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(expectedMediaType, response.Content.Headers.ContentType);
responseContent = responseContent.Trim();
var forgeryTokens = AntiForgeryTestHelper.RetrieveAntiForgeryTokens(responseContent).ToArray();
#if GENERATE_BASELINES
// Reverse usual substitutions and insert format items into the new file content.
for (var index = 0; index < forgeryTokens.Length; index++)
{
responseContent = responseContent.Replace(forgeryTokens[index], $"{{{ index }}}");
}
ResourceFile.UpdateFile(_resourcesAssembly, outputFile, expectedContent, responseContent);
#else
expectedContent = string.Format(expectedContent, forgeryTokens);
Assert.Equal(expectedContent.Trim(), responseContent);
#endif
}
示例6: FormTagHelper_GeneratesExpectedContent
public async Task FormTagHelper_GeneratesExpectedContent(bool? optionsAntiForgery)
{
// Arrange
var newServices = new ServiceCollection();
newServices.InitializeTagHelper<FormTagHelper>((helper, _) => helper.AntiForgery = optionsAntiForgery);
var server = TestHelper.CreateServer(_app, SiteName,
services =>
{
services.Add(newServices);
_configureServices(services);
});
var client = server.CreateClient();
var expectedMediaType = MediaTypeHeaderValue.Parse("text/html; charset=utf-8");
// The K runtime compiles every file under compiler/resources as a resource at runtime with the same name
// as the file name, in order to update a baseline you just need to change the file in that folder.
var resourceName = string.Format(
"compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home.Form.Options.AntiForgery.{0}.html",
optionsAntiForgery?.ToString() ?? "null"
);
var expectedContent = await _resourcesAssembly.ReadResourceAsStringAsync(resourceName);
// Act
// The host is not important as everything runs in memory and tests are isolated from each other.
var response = await client.GetAsync("http://localhost/MvcTagHelper_Home/Form");
var responseContent = await response.Content.ReadAsStringAsync();
var forgeryTokens = AntiForgeryTestHelper.RetrieveAntiForgeryTokens(responseContent);
expectedContent = string.Format(expectedContent, forgeryTokens.ToArray());
// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(expectedMediaType, response.Content.Headers.ContentType);
Assert.Equal(expectedContent.Trim(), responseContent.Trim());
}