简体   繁体   English

使用c#在asp.net中动态创建网页

[英]Dynamically create web page in asp.net using c#

I have ASP.NET web page (mypage.aspx) which has a TextBox (multiline) and a Button. 我有ASP.NET网页(mypage.aspx),它有一个TextBox (多行)和一个Button。

Problem: I want to add html content into textbox and then click the button, it should generate the exact web page according to my html. 问题:我想将html内容添加到文本框中然后单击按钮,它应该根据我的html生成确切的网页。

Example: 例:

<html>
<head><title></title></head>
<body>
<h1>Hello</h1>
</body>
</html>

The generated web page should contain Hello. 生成的网页应包含Hello。

Any idea..? 任何想法..?

Thank in advance. 预先感谢。

Take a look in this answer I gave some while ago, in your case have body be txtHTML.Text to take the HTML given by the user. 采取一看这个答案我给一些前一阵子,你的情况有bodytxtHTML.Text采取由用户提供的HTML。

To make it work you'll have to follow those steps: 要使其工作,您必须遵循以下步骤:

  1. Add this under system.web in the web.config file: web.config文件中的system.web下添加:
    <httpRuntime requestValidationMode="2.0" />
  2. Add this to the Page directive in the .aspx file: 将其添加到.aspx文件中的Page指令:
    validateRequest="false"

Otherwise you won't be able to send raw HTML contents. 否则,您将无法发送原始HTML内容。

Use a html editor like tincymce instead of a textbox. 使用像tincymce这样的html编辑器而不是文本框。 Save the content of the html editor in a database and call it with another page. 将html编辑器的内容保存在数据库中,并使用其他页面调用它。 Or post the content on button press and display it on postback (and hide the textbox + button) 或者按下按钮发布内容并在回发时显示(并隐藏文本框+按钮)

This sounds like quite a simple HelloWorld type app? 这听起来像一个简单的HelloWorld类型的应用程序? Though you'll need to protect against injections if this is a public app (maybe HTMLEncoding and decoding). 虽然这是一个公共应用程序(可能是HTMLEncoding和解码),但您需要防止注入。 But Basically (I'm assuming webforms for the textbox, but it can easily be changed for MVC): 但基本上(我假设文本框的webforms,但它可以很容易地更改为MVC):

This would go in your page: 这将在您的页面中:

In your codebehind (just in the Page_Load method): string content = HtmlContent.Text; 在你的代码隐藏中(仅在Page_Load方法中):string content = HtmlContent.Text;

You can then do what you need to with the content variable. 然后,您可以使用内容变量执行所需操作。 If you are just outputting to the same page add: 如果您只是输出到同一页面添加:

to your page 到你的页面

and change your codebehind from: string content = HtmlContent.Text; 并改变你的代码隐藏:string content = HtmlContent.Text;

to Output.Text = HtmlContent.Text; to Output.Text = HtmlContent.Text;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM