简体   繁体   English

如何使用asp.net生成并以字符串形式返回HTML文档(在Web上下文之外)

[英]How can I use asp.net to generate and return an HTML document as a string (outside of a web context)

I need to write a system to generate HTML email from a data model - 我需要编写一个从数据模型生成HTML电子邮件的系统-

I was going to create a templating system to build the model into an HTML representation using HTML 'fragments' stored in an xml template. 我打算创建一个模板系统,以使用存储在xml模板中的HTML“片段”将模型构建为HTML表示形式。 But it occurs to me that these it might be better to use asp or asp.net than write my own templating system? 但是我想到,使用ASP或ASP.NET可能比编写自己的模板系统更好。

What I am wondering is whether/how it would be possible to use asp (maybe asp.net mvc?) to return an HTML string - I wouldn't be running on a web server, or in response to an HTTP request. 我想知道的是,是否/如何能够使用asp(也许是asp.net mvc?)返回HTML字符串-我不会在Web服务器上运行,也不会响应HTTP请求。

I have not done any asp or asp.net yet- My experience of ASP stretches to 'Create new project' in visual studio - but maybe now is a good time to learn! 我还没有做过ASP或ASP.NET,我的ASP经验可以延伸到Visual Studio中的“创建新项目”,但是现在也许是学习的好时机!

Thank You! 谢谢!

The simplest way is to make an aspx page that renders the email and then read it on the server using WebClient or and HttpWebRequest. 最简单的方法是制作一个呈现电子邮件的aspx页面,然后使用WebClient或和HttpWebRequest在服务器上读取它。

System.Net.WebClient oClient = new System.Net.WebClient();
string Email = oClient.DownloadString(UrlOfPage);

There are other ways to capture the output and I am sure if you search on Google you can find articles about this, but from personal experience this is the simplest way to go. 还有其他捕获输出的方法,我敢肯定,如果您在Google上搜索,可以找到有关此内容的文章,但是从个人经验来看,这是最简单的方法。

Also beware of the Html/Css limitations of many email clients. 还请注意许多电子邮件客户端的HTML / CSS限制。 It is not the same as a browser. 它与浏览器不同。

The standard ASP.NET view engine--ASP.NET web forms--is very difficult to use in this way as it is pretty tied to the HttpContext and really don't want to give you a string back but rather stream into the HttpResponse. 标准的ASP.NET视图引擎-ASP.NET Web表单-很难以这种方式使用,因为它与HttpContext紧密相关,并且确实不想给您返回字符串,而是流式传输到HttpResponse 。 So you'd generally need IIS stood up to get it to go. 因此,通常需要IIS站起来才能使用它。

Xslt (as you are thinking) is a pretty decent option. Xslt(您正在考虑)是一个不错的选择。 As is, if things are simple enough, your own template replacement scheme. 照原样,如果事情很简单,则使用您自己的模板替换方案。 Now, if things are complex enough, some other options would include: 现在,如果事情足够复杂,那么其他一些选择将包括:

Either of those should let you get a string out of a template without too much trouble. 这些中的任何一个都应该让您从模板中获取字符串而不会带来太多麻烦。

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

相关问题 如何在web.config之外的ASP.Net中保留连接字符串? - How can I persist a connection string in ASP.Net outside of web.config? 如何在模板中生成包含现有文本的word文档docx,但是通过asp.net Web表单填写值? - How can i generate a word document docx with existing text as in a template, but fill in value thru a asp.net web form? 如何在 asp.net 中将字符串呈现为 html? - How can I render string as html in asp.net? 我可以从ASP.NET应用程序外的ASP.NET页面对象中呈现html吗? - Can I render html from ASP.NET Page objects outside ASP.NET applications? 使用 ASP.NET Core 的 ViewEngine 在 Controller 外生成 HTML - Use ASP.NET Core's ViewEngine to generate HTML outside Controller 如何在我的 ASP.NET Core 应用程序中使用 Startup.cs 之外的服务? - How can I use a service outside of Startup.cs in my ASP.NET Core Application? 如何从asp.net Web表单返回列表对象到ajax回调函数? - How can I return list object to ajax call back function from asp.net web form? 如何从ASP.NET Web服务返回原始字节? - How can i return raw bytes from ASP.NET web service? ASP.Net/C# 如何存储方法字符串返回以供以后在另一个方法中使用? - ASP.Net/C# How can method string return be stored for later use in another method? 如何在ASP.NET中动态生成视频 - How can I generate video dynamically in ASP.NET
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM