Asp.net mvc 实现.net mvc BeginLabel之类的BeginNotify response.write问题
我需要为Mvc推出自己的BeginLabel帮助程序。我从Mvc源代码中检查/窃取了html.beginForm/ajax.beginForm方法的概念Asp.net mvc 实现.net mvc BeginLabel之类的BeginNotify response.write问题,asp.net-mvc,label,response.write,Asp.net Mvc,Label,Response.write,我需要为Mvc推出自己的BeginLabel帮助程序。我从Mvc源代码中检查/窃取了html.beginForm/ajax.beginForm方法的概念 public static Label BeginLabel(this HtmlHelper htmlHelper) { TagBuilder tagBuilder = new TagBuilder("label"); HttpResponseBase response = htmlHelper.ViewContext.Http
public static Label BeginLabel(this HtmlHelper htmlHelper)
{
TagBuilder tagBuilder = new TagBuilder("label");
HttpResponseBase response = htmlHelper.ViewContext.HttpContext.Response;
response.Write(tagBuilder.ToString(TagRenderMode.StartTag));
return new Label(response);
}
标签仅实现IDisposable接口,以关闭标签:
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
_disposed = true;
_httpResponse.Write("</label>");
}
}
看起来我遗漏了一些东西,因为标签总是在html的顶部呈现,虽然这对我来说很明显,因为我正在编写响应,但我看不出本机BeginForm()是如何实现这一点的。有人能解释一下吗?公共类MvcLabel:IDisposable
public class MvcLabel : IDisposable
{
// Fields
private bool _disposed;
private readonly TextWriter _writer;
public MvcLabel(ViewContext viewContext)
{
if (viewContext == null)
{
throw new ArgumentNullException("viewContext");
}
this._writer = viewContext.Writer;
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!this._disposed)
{
this._disposed = true;
this._writer.Write("</label>");
}
}
public void EndLabel()
{
this.Dispose(true);
}
}
{
//田地
私人住宅;
专用只读文本编写器;
公共MvcLabel(ViewContext ViewContext)
{
if(viewContext==null)
{
抛出新ArgumentNullException(“viewContext”);
}
这是.\u writer=viewContext.writer;
}
公共空间处置()
{
这个。处置(真实);
总干事(本);
}
受保护的虚拟void Dispose(bool disposing)
{
如果(!此._已处理)
{
这是真的;
这个._writer.写(“”);
}
}
公共void EndLabel()
{
这个。处置(真实);
}
}
及
公共静态类HtmlHelperExtension
{
//方法
公共静态MvcLabel BEGINLABLE(此HtmlHelper html,字符串表达式)
{
返回html.BeginLabel(表达式,null);
}
公共静态MvcLabel BEGINLABLE(此HtmlHelper html、字符串表达式、字符串标签文本)
{
返回LabelHelper(html,ModelMetadata.FromStringExpression(expression,html.ViewData),expression,labelText);
}
公共静态MvcLabel BeginLabelFor(此HtmlHelper html,表达式)
{
返回html.BeginLabelFor(表达式,null);
}
公共静态MvcLabel BeginLabelFor(此HtmlHelper html、表达式、字符串labelText)
{
返回LabelHelper(html、ModelMetadata.FromLambdaExpression(表达式、html.ViewData)、ExpressionHelper.GetExpressionText(表达式)、labelText);
}
公共静态MvcLabel BeginLabelForModel(此HtmlHelper html)
{
返回html.BeginLabelForModel(null);
}
公共静态MvcLabel BeginLabelForModel(此HtmlHelper html,字符串labelText)
{
返回LabelHelper(html、html.ViewData.ModelMetadata、string.Empty、labelText);
}
公共静态void EndLabel(此HtmlHelper HtmlHelper)
{
htmlHelper.ViewContext.Writer.Write(“”);
}
内部静态MvcLabel LabelHelper(HtmlHelper html,ModelMetadata元数据,字符串htmlFieldName,字符串labelText=null)
{
string str=labelText??(metadata.DisplayName??(metadata.PropertyName??htmlFieldName.Split(新字符[]{.'}).Last());
标记生成器标记生成器=新标记生成器(“标签”);
tagBuilder.Attributes.Add(“for”,tagBuilder.CreateSanitizedId(html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName));
Write(tagBuilder.ToString(TagRenderMode.StartTag));
如果(!string.IsNullOrEmpty(str))
{
标记生成器=新标记生成器(“span”);
tagBuilder.SetInnerText(str);
Write(tagBuilder.ToString(TagRenderMode.Normal));
}
返回新的MvcLabel(html.ViewContext);
}
}
希望我能帮助其他人…看起来这项工作正在被覆盖,似乎为了这么小的利益做了大量的工作。另外,我不知道你从中得到了什么。没有任何文本的标签?另外,从语义上讲,我也不确定您是否希望在标签中包含验证消息。这让我感到困惑,作者希望利用标签标签包含元素的功能,而不是使用“for”属性。我也不知道为什么要在其中添加跨距。是的,这不太正确,但不需要进行很多更改才能正确。这也让我感到困惑,为什么人们在没有非托管资源的情况下使用2阶段dispose模式。MS代码中也是这样。这就像一个永存的神话!在看到您的解决方案之前,我也做了同样的事情:
public class MvcLabel : IDisposable
{
// Fields
private bool _disposed;
private readonly TextWriter _writer;
public MvcLabel(ViewContext viewContext)
{
if (viewContext == null)
{
throw new ArgumentNullException("viewContext");
}
this._writer = viewContext.Writer;
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!this._disposed)
{
this._disposed = true;
this._writer.Write("</label>");
}
}
public void EndLabel()
{
this.Dispose(true);
}
}
public static class HtmlHelperExtension
{
// Methods
public static MvcLabel BeginLabel(this HtmlHelper html, string expression)
{
return html.BeginLabel(expression, null);
}
public static MvcLabel BeginLabel(this HtmlHelper html, string expression, string labelText)
{
return LabelHelper(html, ModelMetadata.FromStringExpression(expression, html.ViewData), expression, labelText);
}
public static MvcLabel BeginLabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression)
{
return html.BeginLabelFor<TModel, TValue>(expression, null);
}
public static MvcLabel BeginLabelFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, string labelText)
{
return LabelHelper(html, ModelMetadata.FromLambdaExpression<TModel, TValue>(expression, html.ViewData), ExpressionHelper.GetExpressionText(expression), labelText);
}
public static MvcLabel BeginLabelForModel(this HtmlHelper html)
{
return html.BeginLabelForModel(null);
}
public static MvcLabel BeginLabelForModel(this HtmlHelper html, string labelText)
{
return LabelHelper(html, html.ViewData.ModelMetadata, string.Empty, labelText);
}
public static void EndLabel(this HtmlHelper htmlHelper)
{
htmlHelper.ViewContext.Writer.Write("</label>");
}
internal static MvcLabel LabelHelper(HtmlHelper html, ModelMetadata metadata, string htmlFieldName, string labelText = null)
{
string str = labelText ?? (metadata.DisplayName ?? (metadata.PropertyName ?? htmlFieldName.Split(new char[] { '.' }).Last<string>()));
TagBuilder tagBuilder = new TagBuilder("label");
tagBuilder.Attributes.Add("for", TagBuilder.CreateSanitizedId(html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName)));
html.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.StartTag));
if (!string.IsNullOrEmpty(str))
{
tagBuilder = new TagBuilder("span");
tagBuilder.SetInnerText(str);
html.ViewContext.Writer.Write(tagBuilder.ToString(TagRenderMode.Normal));
}
return new MvcLabel(html.ViewContext);
}
}