尝试从Levelnis的博客实现消息类时出现编译错误。
有问题的课程是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc.Rendering;
namespace Notifier.Helpers.MessageNotifier
{
public class Message
{
public MessageSeverity Severity { get; set; }
public string Text { get; set; }
public string Generate()
{
var isDismissable = Severity != MessageSeverity.Danger;
if (Severity == MessageSeverity.None) Severity = MessageSeverity.Info;
var sb = new StringBuilder();
var divTag = new TagBuilder("div");
divTag.AddCssClass("alert fade in");
divTag.AddCssClass("alert-" + Severity.ToString().ToLower());
var spanTag = new TagBuilder("span");
spanTag.MergeAttribute("id", "MessageContent");
if (isDismissable)
{
divTag.AddCssClass("alert-dismissable");
}
sb.Append(divTag.ToString(TagRenderMode.StartTag));
if (isDismissable)
{
var buttonTag = new TagBuilder("button");
buttonTag.MergeAttribute("class", "close");
buttonTag.MergeAttribute("data-dismiss", "alert");
buttonTag.MergeAttribute("aria-hidden", "true");
buttonTag.InnerHtml = "×";
sb.Append(buttonTag.ToString(TagRenderMode.Normal));
}
sb.Append(spanTag.ToString(TagRenderMode.StartTag));
sb.Append(Text);
sb.Append(spanTag.ToString(TagRenderMode.EndTag));
sb.Append(divTag.ToString(TagRenderMode.EndTag));
return sb.ToString();
}
}
}
我收到两个错误 - 第一个与在线上的“.ToString”有关:
sb.Append(divTag.ToString(TagRenderMode.StartTag));
它说:
No overload for method 'ToString' takes 1 arguments
第二个错误与该行有关:
buttonTag.InnerHtml = "×";
它说:
Property or indexer 'TagBuilder.InnerHtml' cannot be assigned to -- it is read only
我已经查看了文档和其他帖子,但到目前为止我还没有找到任何可以帮助我修改它以正确编译的东西......
这些方法在 Asp.net-core 中的实现方式是否不同,如果不是,有人可以建议另一种方法来执行这些行吗?