Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
524 views
in Technique[技术] by (71.8m points)

c# - Compiling an ASPX page into a standalone program

As I mentioned here, I'm trying to generate HTML from an ASPX page inside a WinForms.

I'm trying to compile the ASPX page directly into the EXE; I'd like to be able to write something like this:

var page = new ASP.MyPageName();
var stringWriter = new StringWriter();
using(var htmlWriter = new HtmlTextWriter(stringWriter))
    page.RenderControl(htmlWriter);

I added an ASPX page, set the Build Action to Compile, and put in the following Page declaration:

<%@ Page Language="C#" ClassName="MyPageName" %>

The code compiles, and the properties that I defined in the ASPX are usable from the calling code, but the StringWriter remains empty. I tried calling htmlWriter.Flush, and it didn't help.

The page instance's Controls collection is empty, and it probably shouldn't be.
I looked at the EXE in Reflector and I couldn't find the page content anywhere. I therefore assume that the page isn't being compiled properly.

What is the correct way to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I ended up using ApplicationHost.CreateApplicationHost to run the entire application in the ASP.Net AppDomain. This is far simpler and more reliable than my attempt to fake the ASP.Net AppDomain.

Note: In order to do this, you must put a copy of your EXE file (or whatever assembly contains the type passed to CreateApplicationHost) in your ASP.Net folder's Bin directory. This can be done in a post-build step. You can then handle AssemblyResolve to locate other assemblies in the original directory.

Alternatively, you can place the program itself and all DLLs in the ASP.Net's Bin directory.

NOTE: WinForms' Settings feature will not work in an ASP.Net AppDomain.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...