如何从 ASP.NET MVC 中生成 HTML?

发布于 2024-09-10 15:23:30 字数 734 浏览 3 评论 0原文

我的英语很糟糕,所以我无法解释得很清楚。我会尽力的。

请问可以从 ASP.NET MVC 中生成 HTML 代码吗?

这是控制器

public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewData["Message"] = "Hello ASP.NET MVC!";
        ViewData["author"] = "Author: Alex";


        return View();
    }

这是视图,它是一个模板。

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
title

<%: ViewData["消息"] %>

<%=ViewData["作者"]%>

<%=Html.ActionLink("This is a link.", "Index", "About") %>

我想用这个模板生成html文件。


祝你有美好的一天,

亚历克斯

My english very terrible, so i cant explain very clear. I will try my best.

is it possible to generate HTML code from within ASP.NET MVC, please?

This is Controller

public class HomeController : Controller
{
    public ActionResult Index()
    {
        ViewData["Message"] = "Hello ASP.NET MVC!";
        ViewData["author"] = "Author: Alex";


        return View();
    }

This is Views, It's a template.

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
title

<%: ViewData["Message"] %>

<%=ViewData["author"]%>

<%=Html.ActionLink("This is a link.", "Index", "About") %>

I want to use this template generate html file.


Have a nice day,

Alex

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丑疤怪 2024-09-17 15:23:30

通过生成 html,我假设您指的是以编程方式,而不是指当然可以放入 html 的视图。

在 html 帮助器或控制器中,您可以使用 TagBuilder 来生成 html。例如..

TagBuilder tag = new TagBuilder("img");
tag.Attributes.Add("id", "myImage");
tag.Attributes.Add("src", "/Content/Images/some_image.png");
tag.Attributes.Add("alt", "my image");
tag.Attributes.Add("width", "300");
tag.Attributes.Add("height", "300");

string html = tag.ToString();

您还可以使用有点类似的 HtmlTextWriter。

HtmlTextWriter writer = new HtmlTextWriter(stream);
writer.RenderBeginTag(HtmlTextWriterTag.Ul)
// dome some stuff
writer.RenderEndTag();

ETC。

By generate html I'm assuming you mean programmatically and are not referring to views which of course you can put html in.

In a html helper or controller you can use the TagBuilder to generate html. For example..

TagBuilder tag = new TagBuilder("img");
tag.Attributes.Add("id", "myImage");
tag.Attributes.Add("src", "/Content/Images/some_image.png");
tag.Attributes.Add("alt", "my image");
tag.Attributes.Add("width", "300");
tag.Attributes.Add("height", "300");

string html = tag.ToString();

You can also use HtmlTextWriter which is a little bit similar.

HtmlTextWriter writer = new HtmlTextWriter(stream);
writer.RenderBeginTag(HtmlTextWriterTag.Ul)
// dome some stuff
writer.RenderEndTag();

etc.

沩ん囻菔务 2024-09-17 15:23:30

是的。

您可以通过将 C# 代码添加到视图来生成 HTML。

Yes.

You can generate HTML by adding C# code to a view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文