C# 使用 TagBuilder 和 ASP.NET MVC 4(使用 Razor 引擎)渲染 html 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15450646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:52:55  来源:igfitidea点击:

Rendering html code using TagBuilder and ASP.NET MVC 4 (with Razor engine)

c#razorasp.net-mvc-4

提问by Snake Eyes

I would like to render liitems using TagBuilder.

我想li使用 TagBuilder呈现项目。

My function

我的功能

public static string RenderListTag(this HtmlHelper helper, string labelText, string action, string controller, bool isAdmin, string listCssClass = "")
        {
            string value = string.Empty;

            TagBuilder li = new TagBuilder("li");
            TagBuilder anchor = new TagBuilder("a");
            UrlHelper urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);

            if (string.IsNullOrEmpty(action) || string.IsNullOrEmpty(controller))
            {
                anchor.MergeAttribute("href", "#");
            }
            else
            {
                anchor.MergeAttribute("href", urlHelper.Action(action, controller, new
                {
                    area = isAdmin ? "Admin" : ""
                }));
            }

            anchor.SetInnerText(labelText);

            if (action.IsEqualWith(helper.ViewContext.RouteData.Values["action"].ToString()))
            {
                li.MergeAttribute("class", "active");
            }

            if (!string.IsNullOrEmpty(listCssClass))
            {
                li.MergeAttribute("class", listCssClass);
            }

            li.SetInnerText(anchor.ToString(TagRenderMode.Normal));

            return li.ToString(TagRenderMode.Normal);
        }

When I call using the following code:

当我使用以下代码调用时:

@Html.RenderListTag("Home", "Index", "Contents", false)
@Html.RenderListTag("About", "About", "Home", false)
@Html.RenderListTag("Contact", "Contact", "Home", false)
@Html.RenderListTag("Show toolbar", "", "", false, "options no-display")
@Html.RenderListTag("CMS", "Index", "Home", true)

The results is printed as text NOT html tag.

结果打印为 text NOT html 标签。

<li class="active">&lt;a href=&quot;/Contents&quot;&gt;Home&lt;/a&gt;</li> <li>&lt;a href=&quot;/Home/About&quot;&gt;About&lt;/a&gt;</li> <li>&lt;a href=&quot;/Home/Contact&quot;&gt;Contact&lt;/a&gt;</li> <li class="options no-display">&lt;a href=&quot;#&quot;&gt;Show toolbar&lt;/a&gt;</li> <li class="active">&lt;a href=&quot;/Admin/Home&quot;&gt;CMS&lt;/a&gt;</li> 

I want to print the HTML tag not text.

我想打印 HTML 标签而不是文本。

Where is my mistake ?

我的错误在哪里?

采纳答案by Mick Walker

Use @Html.Raw(Html.RenderListTag("CMS", "Index", "Home", true))

@Html.Raw(Html.RenderListTag("CMS", "Index", "Home", true))

回答by Carlos Martinez T

Try changing the last line to:

尝试将最后一行更改为:

return helper.Raw(li.ToString(TagRenderMode.Normal)).ToHtmlString();

return helper.Raw(li.ToString(TagRenderMode.Normal)).ToHtmlString();

回答by Snake Eyes

I found my mistake :)

我发现我的错误:)

I used

我用了

li.SetInnerText(anchor.ToString(TagRenderMode.Normal));

The correct way is

正确的方法是

li.InnerHtml = anchor.ToString(TagRenderMode.Normal);

I changed type of my function from stringto MvcHtmlStringlike:

我将我的函数类型从更改stringMvcHtmlString喜欢:

public static MvcHtmlString RenderListTag(this HtmlHelper helper, string labelText, string action, string controller, bool isAdmin, string listCssClass = "")

And the return of function is:

而函数的返回是:

return MvcHtmlString.Create(li.ToString());

Now, works.

现在,工作。

回答by Francisco Javier Banos Lemoine

For situations like this, I have a good friend in the static method HtmlDecode of the HttpUtility class. Try: return MvcHtmlString.Create(HttpUtility.HtmlDecode(li.ToString(TagRenderMode.Normal)));

对于这种情况,我在 HttpUtility 类的静态方法 HtmlDecode 中有一个好朋友。尝试: return MvcHtmlString.Create(HttpUtility.HtmlDecode(li.ToString(TagRenderMode.Normal)));

HTH

HTH

回答by Thregarth

You must return a MvcHtmlStringfrom method

您必须从方法返回一个MvcHtmlString

public static MvcHtmlString RenderListTag(this HtmlHelper helper, string labelText, string action, string controller, bool isAdmin, string listCssClass = "")
{
    string value = string.Empty;

    TagBuilder li = new TagBuilder("li");
    TagBuilder anchor = new TagBuilder("a");
    UrlHelper urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);

    if (string.IsNullOrEmpty(action) || string.IsNullOrEmpty(controller))
    {
        anchor.MergeAttribute("href", "#");
    }
    else
    {
        anchor.MergeAttribute("href", urlHelper.Action(action, controller, new
        {
            area = isAdmin ? "Admin" : ""
        }));
    }

    anchor.SetInnerText(labelText);

    if (action.IsEqualWith(helper.ViewContext.RouteData.Values["action"].ToString()))
    {
        li.MergeAttribute("class", "active");
    }

    if (!string.IsNullOrEmpty(listCssClass))
    {
        li.MergeAttribute("class", listCssClass);
    }

    li.SetInnerText(anchor.ToString(TagRenderMode.Normal));

    return new MvcHtmlString(li.ToString(TagRenderMode.Normal));
}