Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling an ASPX page into a standalone program

Tags:

c#

.net

asp.net

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?

like image 459
SLaks Avatar asked Dec 01 '25 09:12

SLaks


2 Answers

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.

like image 86
SLaks Avatar answered Dec 04 '25 01:12

SLaks


I believe what you want to use is the SimpleWorkerRequest.

Unfortunately, however, it requires that the resource (I believe) live on disk. From your description it sounds like you prefered for the whole app to reside in your DLL. If that is the case you will most likely need to implement your own HttpWorkerRequest.

like image 25
Jason Whitehorn Avatar answered Dec 04 '25 00:12

Jason Whitehorn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!