编译一个ASPX页面转换成一个独立的程序 [英] Compiling an ASPX page into a standalone program

查看:143
本文介绍了编译一个ASPX页面转换成一个独立的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我所提到这里,我想从一个ASPX生成HTML里面的WinForms页面。

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

我想直接编译的ASPX页面到EXE;我希望能够写这样的:

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);

我添加了一个ASPX页面,设置生成操作编译,并把下面的声明:

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

在code编译,而且我在ASPX定义的属性与调用code可用,但StringWriter的仍然是空的。我打过电话 htmlWriter.Flush ,它并没有帮助。

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.

实例的控件集合为空,它可能不应该。
我看着EXE的反射器,我无法找到页面内容的任何地方。因此,我认为页面不被编译正确。

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?

推荐答案

我最终使用<一个href="http://msdn.microsoft.com/en-us/library/system.web.hosting.applicationhost.createapplicationhost.aspx"相对=nofollow> ApplicationHost.CreateApplicationHost 运行在ASP.Net的AppDomain整个应用程序。这是简单得多,比我试图伪造ASP.Net的AppDomain。

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.

注:为了做到这一点,你必须把你的EXE文件的副本(或任何组件包含传递给CreateApplicationHost的类型)在ASP.Net文件夹的Bin目录。这可以在生成后步骤来完成。然后,您可以处理 AssemblyResolve 定位其他组件在原有的目录。

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.

另外,你可以把程序本身和ASP.Net的目录中的所有DLL文件。

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

注意:WinForms的设置功能将不会在ASP.Net的AppDomain工作

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

这篇关于编译一个ASPX页面转换成一个独立的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆