本文整理汇总了C#中Microsoft.AspNet.Razor.Runtime.TagHelpers.DefaultTagHelperContent.SetContent方法的典型用法代码示例。如果您正苦于以下问题:C# DefaultTagHelperContent.SetContent方法的具体用法?C# DefaultTagHelperContent.SetContent怎么用?C# DefaultTagHelperContent.SetContent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.AspNet.Razor.Runtime.TagHelpers.DefaultTagHelperContent
的用法示例。
在下文中一共展示了DefaultTagHelperContent.SetContent方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetContentClearsExistingContent
public void SetContentClearsExistingContent()
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
var expected = "Hello World!";
tagHelperContent.SetContent("Contoso");
// Act
tagHelperContent.SetContent(expected);
// Assert
Assert.Equal(expected, tagHelperContent.GetContent());
}
示例2: SetContentClearsExistingContent
public void SetContentClearsExistingContent()
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
var expected = "HtmlEncode[[Hello World!]]";
tagHelperContent.SetContent("Contoso");
// Act
tagHelperContent.SetContent("Hello World!");
// Assert
Assert.Equal(expected, tagHelperContent.GetContent(new CommonTestEncoder()));
}
示例3: 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()));
}
示例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());
}
示例5: ProcessAsync_GeneratesExpectedOutput
public async Task ProcessAsync_GeneratesExpectedOutput()
{
// Arrange
var expectedTagName = "not-div";
var metadataProvider = new TestModelMetadataProvider();
var htmlGenerator = new TestableHtmlGenerator(metadataProvider);
var validationSummaryTagHelper = new ValidationSummaryTagHelper(htmlGenerator)
{
ValidationSummary = ValidationSummary.All,
};
var expectedPreContent = "original pre-content";
var expectedContent = "original content";
var tagHelperContext = new TagHelperContext(
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(),
uniqueId: "test",
getChildContentAsync: useCachedResult =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
var output = new TagHelperOutput(
expectedTagName,
attributes: new TagHelperAttributeList
{
{ "class", "form-control" }
});
output.PreContent.SetContent(expectedPreContent);
output.Content.SetContent(expectedContent);
output.PostContent.SetContent("Custom Content");
Model model = null;
var viewContext = TestableHtmlGenerator.GetViewContext(model, htmlGenerator, metadataProvider);
validationSummaryTagHelper.ViewContext = viewContext;
// Act
await validationSummaryTagHelper.ProcessAsync(tagHelperContext, output);
// Assert
Assert.Equal(2, output.Attributes.Count);
var attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("class"));
Assert.Equal("form-control validation-summary-valid", attribute.Value);
attribute = Assert.Single(output.Attributes, attr => attr.Name.Equals("data-valmsg-summary"));
Assert.Equal("true", attribute.Value);
Assert.Equal(expectedPreContent, output.PreContent.GetContent());
Assert.Equal(expectedContent, output.Content.GetContent());
Assert.Equal("Custom Content<ul><li style=\"display:none\"></li>" + Environment.NewLine + "</ul>",
output.PostContent.GetContent());
Assert.Equal(expectedTagName, output.TagName);
}
示例6: CanSetContent
public void CanSetContent()
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
var expected = "Hello World!";
// Act
tagHelperContent.SetContent(expected);
// Assert
Assert.Equal(expected, tagHelperContent.GetContent());
}
示例7: SetContent_WithTagHelperContent_WorksAsExpected
public void SetContent_WithTagHelperContent_WorksAsExpected(string expected)
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
var copiedTagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent(expected);
// Act
copiedTagHelperContent.SetContent(tagHelperContent);
// Assert
Assert.Equal(expected, copiedTagHelperContent.GetContent());
}
示例8: ProcessAsync_CallsIntoGenerateFormWithExpectedParameters
public async Task ProcessAsync_CallsIntoGenerateFormWithExpectedParameters()
{
// Arrange
var viewContext = CreateViewContext();
var context = new TagHelperContext(
allAttributes: new Dictionary<string, object>(),
items: new Dictionary<object, object>(),
uniqueId: "test",
getChildContentAsync: () =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
var output = new TagHelperOutput(
"form",
attributes: new Dictionary<string, object>());
var generator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
generator
.Setup(mock => mock.GenerateForm(viewContext, "Index", "Home", null, null, null))
.Returns(new TagBuilder("form", new HtmlEncoder()))
.Verifiable();
var formTagHelper = new FormTagHelper
{
Action = "Index",
AntiForgery = false,
Controller = "Home",
Generator = generator.Object,
ViewContext = viewContext,
};
// Act & Assert
await formTagHelper.ProcessAsync(context, output);
generator.Verify();
Assert.Equal("form", output.TagName);
Assert.False(output.SelfClosing);
Assert.Empty(output.Attributes);
Assert.Empty(output.PreContent.GetContent());
Assert.True(output.Content.IsEmpty);
Assert.Empty(output.PostContent.GetContent());
}
示例9: ProcessAsync_GeneratesExpectedOutput
public async Task ProcessAsync_GeneratesExpectedOutput()
{
// Arrange
var expectedTagName = "not-form";
var metadataProvider = new TestModelMetadataProvider();
var tagHelperContext = new TagHelperContext(
allAttributes: new Dictionary<string, object>
{
{ "id", "myform" },
{ "asp-route-foo", "bar" },
{ "asp-action", "index" },
{ "asp-controller", "home" },
{ "method", "post" },
{ "asp-anti-forgery", true }
},
items: new Dictionary<object, object>(),
uniqueId: "test",
getChildContentAsync: () =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something Else");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
var output = new TagHelperOutput(
expectedTagName,
attributes: new Dictionary<string, object>
{
{ "id", "myform" },
{ "asp-route-foo", "bar" },
});
output.PostContent.SetContent("Something");
var urlHelper = new Mock<IUrlHelper>();
urlHelper
.Setup(mock => mock.Action(It.IsAny<UrlActionContext>())).Returns("home/index");
var htmlGenerator = new TestableHtmlGenerator(metadataProvider, urlHelper.Object);
var viewContext = TestableHtmlGenerator.GetViewContext(model: null,
htmlGenerator: htmlGenerator,
metadataProvider: metadataProvider);
var expectedPostContent = "Something" + htmlGenerator.GenerateAntiForgery(viewContext)
.ToString(TagRenderMode.SelfClosing);
var formTagHelper = new FormTagHelper
{
Action = "index",
AntiForgery = true,
Controller = "home",
Generator = htmlGenerator,
ViewContext = viewContext,
};
// Act
await formTagHelper.ProcessAsync(tagHelperContext, output);
// Assert
Assert.Equal(3, output.Attributes.Count);
var attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("id"));
Assert.Equal("myform", attribute.Value);
attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("method"));
Assert.Equal("post", attribute.Value);
attribute = Assert.Single(output.Attributes, kvp => kvp.Key.Equals("action"));
Assert.Equal("home/index", attribute.Value);
Assert.Empty(output.PreContent.GetContent());
Assert.True(output.Content.IsEmpty);
Assert.Equal(expectedPostContent, output.PostContent.GetContent());
Assert.Equal(expectedTagName, output.TagName);
}
示例10: Fluent_SetContent_AppendFormat_Append_WritesExpectedContent
public void Fluent_SetContent_AppendFormat_Append_WritesExpectedContent()
{
// Arrange
var tagHelperContent = new DefaultTagHelperContent();
var expected = new[] { "First ", "Second Third ", "Fourth" };
var i = 0;
// Act
tagHelperContent
.SetContent("First ")
.AppendFormat("{0} Third ", "Second")
.Append("Fourth");
// Assert
foreach (var value in tagHelperContent)
{
Assert.Equal(expected[i++], value);
}
}
示例11: MakeTagHelperContext
private TagHelperContext MakeTagHelperContext(
IDictionary<string, object> attributes = null,
string content = null)
{
attributes = attributes ?? new Dictionary<string, object>();
return new TagHelperContext(
attributes,
items: new Dictionary<object, object>(),
uniqueId: Guid.NewGuid().ToString("N"),
getChildContentAsync: () =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent(content);
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
}
示例12: Process_GeneratesExpectedOutput
public async Task Process_GeneratesExpectedOutput(
object container,
Type containerType,
object model,
NameAndId nameAndId,
string expectedContent)
{
// Arrange
var expectedAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
{ "id", nameAndId.Id },
{ "name", nameAndId.Name },
{ "valid", "from validation attributes" },
};
var expectedTagName = "not-textarea";
var metadataProvider = new TestModelMetadataProvider();
var containerMetadata = metadataProvider.GetMetadataForType(containerType);
var containerExplorer = metadataProvider.GetModelExplorerForType(containerType, container);
var propertyMetadata = metadataProvider.GetMetadataForProperty(containerType, "Text");
var modelExplorer = containerExplorer.GetExplorerForExpression(propertyMetadata, model);
var htmlGenerator = new TestableHtmlGenerator(metadataProvider)
{
ValidationAttributes =
{
{ "valid", "from validation attributes" },
}
};
// Property name is either nameof(Model.Text) or nameof(NestedModel.Text).
var modelExpression = new ModelExpression(nameAndId.Name, modelExplorer);
var tagHelper = new TextAreaTagHelper(htmlGenerator)
{
For = modelExpression,
};
var tagHelperContext = new TagHelperContext(
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(),
uniqueId: "test",
getChildContentAsync: useCachedResult =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
var htmlAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
};
var output = new TagHelperOutput(expectedTagName, htmlAttributes)
{
TagMode = TagMode.SelfClosing,
};
output.Content.SetContent("original content");
var viewContext = TestableHtmlGenerator.GetViewContext(model, htmlGenerator, metadataProvider);
tagHelper.ViewContext = viewContext;
// Act
await tagHelper.ProcessAsync(tagHelperContext, output);
// Assert
Assert.Equal(TagMode.SelfClosing, output.TagMode);
Assert.Equal(expectedAttributes, output.Attributes);
Assert.Equal(expectedContent, HtmlContentUtilities.HtmlContentToString(output.Content));
Assert.Equal(expectedTagName, output.TagName);
}
示例13: ProcessAsync_CallsGenerateTextBox_WithExpectedParameters
public async Task ProcessAsync_CallsGenerateTextBox_WithExpectedParameters(
string dataTypeName,
string inputTypeName,
string model)
{
// Arrange
var contextAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
};
if (!string.IsNullOrEmpty(inputTypeName))
{
contextAttributes["type"] = inputTypeName; // Support restoration of type attribute, if any.
}
var expectedAttributes = new TagHelperAttributeList
{
{ "class", "form-control text-control" },
{ "type", inputTypeName ?? "text" }, // Generator restores type attribute; adds "text" if none.
};
var expectedPreContent = "original pre-content";
var expectedContent = "original content";
var expectedPostContent = "original post-content";
var expectedTagName = "not-input";
var context = new TagHelperContext(
allAttributes: contextAttributes,
items: new Dictionary<object, object>(),
uniqueId: "test",
getChildContentAsync: useCachedResult =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
var originalAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
};
var output = new TagHelperOutput(expectedTagName, originalAttributes)
{
TagMode = TagMode.StartTagOnly,
};
output.PreContent.SetContent(expectedPreContent);
output.Content.SetContent(expectedContent);
output.PostContent.SetContent(expectedPostContent);
var metadataProvider = new TestModelMetadataProvider();
metadataProvider.ForProperty<Model>("Text").DisplayDetails(dd => dd.DataTypeName = dataTypeName);
var htmlGenerator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
var tagHelper = GetTagHelper(
htmlGenerator.Object,
model,
nameof(Model.Text),
metadataProvider: metadataProvider);
tagHelper.InputTypeName = inputTypeName;
var tagBuilder = new TagBuilder("input")
{
Attributes =
{
{ "class", "text-control" },
},
};
htmlGenerator
.Setup(mock => mock.GenerateTextBox(
tagHelper.ViewContext,
tagHelper.For.ModelExplorer,
tagHelper.For.Name,
model, // value
null, // format
It.Is<Dictionary<string, object>>(m => m.ContainsKey("type")))) // htmlAttributes
.Returns(tagBuilder)
.Verifiable();
// Act
await tagHelper.ProcessAsync(context, output);
// Assert
htmlGenerator.Verify();
Assert.Equal(TagMode.StartTagOnly, output.TagMode);
Assert.Equal(expectedAttributes, output.Attributes);
Assert.Equal(expectedPreContent, output.PreContent.GetContent());
Assert.Equal(expectedContent, output.Content.GetContent());
Assert.Equal(expectedPostContent, output.PostContent.GetContent());
Assert.Equal(expectedTagName, output.TagName);
}
示例14: ProcessAsync_CallsGenerateRadioButton_WithExpectedParameters
public async Task ProcessAsync_CallsGenerateRadioButton_WithExpectedParameters(
string inputTypeName,
string model)
{
// Arrange
var value = "match"; // Real generator would use this for comparison with For.Metadata.Model.
var contextAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
{ "value", value },
};
if (!string.IsNullOrEmpty(inputTypeName))
{
contextAttributes["type"] = inputTypeName; // Support restoration of type attribute, if any.
}
var expectedAttributes = new TagHelperAttributeList
{
{ "class", "form-control radio-control" },
{ "type", inputTypeName ?? "radio" }, // Generator restores type attribute; adds "radio" if none.
{ "value", value },
};
var expectedPreContent = "original pre-content";
var expectedContent = "original content";
var expectedPostContent = "original post-content";
var expectedTagName = "not-input";
var context = new TagHelperContext(
allAttributes: contextAttributes,
items: new Dictionary<object, object>(),
uniqueId: "test",
getChildContentAsync: useCachedResult =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
var originalAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
};
var output = new TagHelperOutput(expectedTagName, originalAttributes)
{
TagMode = TagMode.StartTagOnly,
};
output.PreContent.SetContent(expectedPreContent);
output.Content.SetContent(expectedContent);
output.PostContent.SetContent(expectedPostContent);
var htmlGenerator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
var tagHelper = GetTagHelper(htmlGenerator.Object, model, nameof(Model.Text));
tagHelper.InputTypeName = inputTypeName;
tagHelper.Value = value;
var tagBuilder = new TagBuilder("input")
{
Attributes =
{
{ "class", "radio-control" },
},
};
htmlGenerator
.Setup(mock => mock.GenerateRadioButton(
tagHelper.ViewContext,
tagHelper.For.ModelExplorer,
tagHelper.For.Name,
value,
null, // isChecked
null)) // htmlAttributes
.Returns(tagBuilder)
.Verifiable();
// Act
await tagHelper.ProcessAsync(context, output);
// Assert
htmlGenerator.Verify();
Assert.Equal(TagMode.StartTagOnly, output.TagMode);
Assert.Equal(expectedAttributes, output.Attributes);
Assert.Equal(expectedPreContent, output.PreContent.GetContent());
Assert.Equal(expectedContent, output.Content.GetContent());
Assert.Equal(expectedPostContent, output.PostContent.GetContent());
Assert.Equal(expectedTagName, output.TagName);
}
示例15: ProcessAsync_CallsGenerateCheckBox_WithExpectedParameters
public async Task ProcessAsync_CallsGenerateCheckBox_WithExpectedParameters()
{
// Arrange
var originalContent = "original content";
var originalTagName = "not-input";
var expectedPreContent = "original pre-content";
var expectedContent = originalContent + "<input class=\"HtmlEncode[[form-control]]\" /><hidden />";
var expectedPostContent = "original post-content";
var context = new TagHelperContext(
allAttributes: new ReadOnlyTagHelperAttributeList<IReadOnlyTagHelperAttribute>(
Enumerable.Empty<IReadOnlyTagHelperAttribute>()),
items: new Dictionary<object, object>(),
uniqueId: "test",
getChildContentAsync: useCachedResult =>
{
var tagHelperContent = new DefaultTagHelperContent();
tagHelperContent.SetContent("Something");
return Task.FromResult<TagHelperContent>(tagHelperContent);
});
var originalAttributes = new TagHelperAttributeList
{
{ "class", "form-control" },
};
var output = new TagHelperOutput(originalTagName, originalAttributes)
{
TagMode = TagMode.SelfClosing,
};
output.PreContent.SetContent(expectedPreContent);
output.Content.SetContent(originalContent);
output.PostContent.SetContent(expectedPostContent);
var htmlGenerator = new Mock<IHtmlGenerator>(MockBehavior.Strict);
var tagHelper = GetTagHelper(htmlGenerator.Object, model: false, propertyName: nameof(Model.IsACar));
var tagBuilder = new TagBuilder("input")
{
Attributes =
{
{ "class", "form-control" },
},
TagRenderMode = TagRenderMode.SelfClosing
};
htmlGenerator
.Setup(mock => mock.GenerateCheckBox(
tagHelper.ViewContext,
tagHelper.For.ModelExplorer,
tagHelper.For.Name,
null, // isChecked
It.IsAny<object>())) // htmlAttributes
.Returns(tagBuilder)
.Verifiable();
htmlGenerator
.Setup(mock => mock.GenerateHiddenForCheckbox(
tagHelper.ViewContext,
tagHelper.For.ModelExplorer,
tagHelper.For.Name))
.Returns(new TagBuilder("hidden") { TagRenderMode = TagRenderMode.SelfClosing })
.Verifiable();
// Act
await tagHelper.ProcessAsync(context, output);
// Assert
htmlGenerator.Verify();
Assert.Empty(output.Attributes); // Moved to Content and cleared
Assert.Equal(expectedPreContent, output.PreContent.GetContent());
Assert.Equal(expectedContent, HtmlContentUtilities.HtmlContentToString(output.Content));
Assert.Equal(expectedPostContent, output.PostContent.GetContent());
Assert.Equal(TagMode.SelfClosing, output.TagMode);
Assert.Null(output.TagName); // Cleared
}