本文整理汇总了C#中TagBuilder.WriteTo方法的典型用法代码示例。如果您正苦于以下问题:C# TagBuilder.WriteTo方法的具体用法?C# TagBuilder.WriteTo怎么用?C# TagBuilder.WriteTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TagBuilder
的用法示例。
在下文中一共展示了TagBuilder.WriteTo方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Process
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "div";
output.PreContent.SetHtmlContent("<ul class=\"pagination pagination-lg\">");
var items = new StringBuilder();
for (var i = 1; i <= TotalPages; i++)
{
var li = new TagBuilder("li");
if (i == CurrentPage)
{
li.AddCssClass("active");
}
var a = new TagBuilder("a");
a.MergeAttribute("href", $"{Url}?page={i}&{AdditionalParameters}");
a.MergeAttribute("title", $"Click to go to page {i}");
a.InnerHtml.AppendHtml(i.ToString());
li.InnerHtml.AppendHtml(a);
var writer = new System.IO.StringWriter();
li.WriteTo(writer, HtmlEncoder.Default);
var s = writer.ToString();
items.AppendLine(s);
}
output.Content.SetHtmlContent(items.ToString());
output.PostContent.SetHtmlContent("</ul>");
output.Attributes.Clear();
output.Attributes.Add("class", "pager");
}
示例2: Process
public override void Process(TagHelperContext context, TagHelperOutput output)
{
//set the tag name to a select...the attributes will carry along automatically. Otherwise grab context.AllAttributes
output.TagName = "select";
using (var writer = new StringWriter())
{
//loop through the items
foreach (var item in SelectItems)
{
//create the option
var option = new TagBuilder("option");
//set the value
option.MergeAttribute("value", item.Value);
//set the text
option.InnerHtml.Append(item.Text);
//write it to the writer
option.WriteTo(writer, encoder);
}
//now output the writer to the page
output.Content.SetHtmlContent(writer.ToString());
}
}
示例3: ProcessAsync
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
if (_bundleManager.Exists(Source))
{
var result = (await _smidgeHelper.GenerateJsUrlsAsync(Source, Debug)).ToArray();
var currAttr = output.Attributes.ToDictionary(x => x.Name, x => x.Value);
using (var writer = new StringWriter())
{
foreach (var s in result)
{
var builder = new TagBuilder(output.TagName)
{
TagRenderMode = TagRenderMode.Normal
};
builder.MergeAttributes(currAttr);
builder.Attributes["src"] = s;
builder.WriteTo(writer, _encoder);
}
writer.Flush();
output.PostElement.SetContent(new HtmlString(writer.ToString()));
}
//This ensures the original tag is not written.
output.TagName = null;
}
else
{
//use what is there
output.Attributes["src"] = Source;
}
}
示例4: RenderForm
/// <summary>
/// This renders out the form for us
/// </summary>
/// <param name="htmlHelper"></param>
/// <param name="formAction"></param>
/// <param name="method"></param>
/// <param name="htmlAttributes"></param>
/// <param name="surfaceController"></param>
/// <param name="surfaceAction"></param>
/// <param name="area"></param>
/// <param name="additionalRouteVals"></param>
/// <returns></returns>
/// <remarks>
/// This code is pretty much the same as the underlying MVC code that writes out the form
/// </remarks>
private static MvcForm RenderForm(this IHtmlHelper htmlHelper,
string formAction,
FormMethod method,
IDictionary<string, object> htmlAttributes,
string surfaceController,
string surfaceAction,
string area,
object additionalRouteVals = null)
{
if (htmlAttributes == null)
{
htmlAttributes = new Dictionary<string, object>();
}
//ensure that the multipart/form-data is added to the html attributes
if (htmlAttributes.ContainsKey("enctype") == false)
{
htmlAttributes.Add("enctype", "multipart/form-data");
}
var tagBuilder = new TagBuilder("form");
tagBuilder.MergeAttributes(htmlAttributes);
// action is implicitly generated, so htmlAttributes take precedence.
tagBuilder.MergeAttribute("action", formAction);
// method is an explicit parameter, so it takes precedence over the htmlAttributes.
tagBuilder.MergeAttribute("method", HtmlHelper.GetFormMethodString(method), true);
tagBuilder.TagRenderMode = TagRenderMode.StartTag;
tagBuilder.WriteTo(htmlHelper.ViewContext.Writer, new HtmlEncoder());
//new UmbracoForm:
var theForm = new UmbracoForm(htmlHelper.UrlEncoder, htmlHelper.ViewContext, surfaceController, surfaceAction, area, additionalRouteVals);
return theForm;
}
示例5: ProcessAsync
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
{
if (!string.IsNullOrWhiteSpace(ModelName) && ModelData != null)
{
modelContext.Initialize(ModelData);
var script = new TagBuilder("script");
script.Attributes.Add("model", ModelName);
script.Attributes.Add("type", "application/json");
if (ModelPersistent)
{
script.Attributes.Add("persistent", "");
}
script.InnerHtml = new StringHtmlContent(jsonHelper.Serialize(ModelData).ToString());
output.Attributes.Add("model", ModelName);
using (var writer = new StringWriter())
{
script.WriteTo(writer, new HtmlEncoder());
output.PreContent.SetContent(writer.ToString());
}
}
else if (!string.IsNullOrWhiteSpace(Scope))
{
output.Attributes.Add("scope", Scope);
modelContext.Scope(Scope);
}
output.Attributes.Add("name", Name);
await base.ProcessAsync(context, output);
}
示例6: Script
public static IHtmlContent Script(this IHtmlHelper helper, string contentPath)
{
if (string.IsNullOrWhiteSpace(contentPath))
{
throw new ArgumentOutOfRangeException(nameof(contentPath), contentPath, "Must not be null or whitespace");
}
var sb = new StringBuilder();
var paths = Configuration == null ? new[] { contentPath } : Configuration.Scripts[contentPath];
foreach (var path in paths)
{
var script = new TagBuilder("script");
script.MergeAttribute("type", "text/javascript");
script.MergeAttribute("src", path);
using (var writer = new StringWriter())
{
script.WriteTo(writer, HtmlEncoder.Default);
sb.AppendLine(writer.ToString());
}
}
return new HtmlString(sb.ToString());
}
示例7: BuildWidget
public static string BuildWidget()
{
using (var stringWriter = new StringWriter())
{
var widget = new TagBuilder("div");
widget.MergeAttribute("class", "g-recaptcha");
widget.MergeAttribute("data-sitekey", ReCaptchaConfiguration.Instance.SiteKey);
widget.TagRenderMode = TagRenderMode.Normal;
widget.WriteTo(stringWriter, HtmlEncoder.Default);
return stringWriter.ToString();
}
}
示例8: BuildScript
public static string BuildScript(string language)
{
using (var stringWriter = new StringWriter())
{
var script = new TagBuilder("script");
if (string.IsNullOrEmpty(language))
{
script.MergeAttribute("src", "https://www.google.com/recaptcha/api.js");
}
else
{
script.MergeAttribute("src", "https://www.google.com/recaptcha/api.js?hl=" + language);
}
script.MergeAttribute("async", string.Empty);
script.MergeAttribute("defer", string.Empty);
script.TagRenderMode = TagRenderMode.Normal;
script.WriteTo(stringWriter, HtmlEncoder.Default);
return stringWriter.ToString();
}
}
示例9: WriteTo_IgnoresIdAttributeCase
public void WriteTo_IgnoresIdAttributeCase(TagRenderMode renderingMode, string expectedOutput)
{
// Arrange
var tagBuilder = new TagBuilder("p");
// An empty value id attribute should not be rendered via ToString.
tagBuilder.Attributes.Add("ID", string.Empty);
tagBuilder.TagRenderMode = renderingMode;
// Act
using (var writer = new StringWriter())
{
tagBuilder.WriteTo(writer, new HtmlTestEncoder());
// Assert
Assert.Equal(expectedOutput, writer.ToString());
}
}
示例10: WriteTo_IncludesInnerHtml
public void WriteTo_IncludesInnerHtml()
{
// Arrange
var tagBuilder = new TagBuilder("p");
tagBuilder.InnerHtml.AppendHtml("<span>Hello</span>");
tagBuilder.InnerHtml.Append(", World!");
// Act
using (var writer = new StringWriter())
{
tagBuilder.WriteTo(writer, new HtmlTestEncoder());
// Assert
Assert.Equal("<p><span>Hello</span>HtmlEncode[[, World!]]</p>", writer.ToString());
}
}
示例11: Process
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.TagName = "ul";
output.Attributes["class"] = "pagination pagination-sm";
if (CurrentPage == 0)
CurrentPage = 1;
var writer = new StringWriter();
var encoder = new HtmlEncoder();
var li = new TagBuilder("li");
var a = new TagBuilder("a");
if (CurrentPage > 10)
{
a.MergeAttribute("href", $"{Url}?Page={((CurrentPage - 1) / 10) * 10}{(SearchMode ? $"&SearchField={SearchField}&SearchQuery={SearchQuery}" : "")}");
a.InnerHtml.AppendHtml("◄");
}
else
{
a.MergeAttribute("class", "disabled");
a.InnerHtml.AppendHtml("◁");
}
li.InnerHtml.Append(a);
li.WriteTo(writer, encoder);
int firstPage = (CurrentPage - 1) / 10 * 10 + 1;
int i;
for (i = firstPage; i < firstPage + 10; i++)
{
if (i > TotalPages)
break;
li = new TagBuilder("li");
a = new TagBuilder("a");
if (i == CurrentPage)
{
li.MergeAttribute("class", "active");
a.MergeAttribute("href", "#");
}
else
{
a.MergeAttribute("href", $"{Url}?Page={i}{(SearchMode ? $"&SearchField={SearchField}&SearchQuery={SearchQuery}" : "")}");
}
a.InnerHtml.AppendHtml(i.ToString());
li.InnerHtml.Append(a);
li.WriteTo(writer, encoder);
}
li = new TagBuilder("li");
a = new TagBuilder("a");
if (i < TotalPages)
{
a.MergeAttribute("href", $"{Url}?Page={i}{(SearchMode ? $"&SearchField={SearchField}&SearchQuery={SearchQuery}" : "")}");
a.InnerHtml.AppendHtml("►");
}
else
{
a.MergeAttribute("class", "disabled");
a.InnerHtml.AppendHtml("▷");
}
li.InnerHtml.Append(a);
li.WriteTo(writer, encoder);
output.Content.AppendHtml(writer.ToString());
}
示例12: WriteTo_WriteEmptyAttribute_WhenValueIsNullOrEmpty
public void WriteTo_WriteEmptyAttribute_WhenValueIsNullOrEmpty(string attributeKey, string attributeValue, string expectedOutput)
{
// Arrange
var tagBuilder = new TagBuilder("p");
// Act
tagBuilder.Attributes.Add(attributeKey, attributeValue);
// Assert
using (var writer = new StringWriter())
{
tagBuilder.WriteTo(writer, new HtmlTestEncoder());
Assert.Equal(expectedOutput, writer.ToString());
}
}