问题
I have a winforms app that I would like to add some new features to.
Ideally I would have a new form that would have an embedded browser control. I want the pages in the browser to be 'served' from the app itself ( as opposed to a remote sever ).
The pages will be dynamically created dependent on data from within the App.
Also, how do I cater for references to assets like CSS, Javascript and Image files. Ideally these would need to be handled by the application as well.
How do I do this?
回答1:
I use this technique in my application. I host a WebBrowser, and I populated it as follows:
public void DisplayHtml(HtmlGenerator gen)
{
webBrowser.DocumentText = gen.GenerateHtmlString());
}
Using this method, I don't have to actually generate a file on my file system with the HTML content.
回答2:
Use the WebBrowser control.
You can set the DocumentText property to the HTML you want to display. (thanks @Anton Semenov).
Alternatively, you can feed it local file URLs from files that your application creates.
回答3:
In my test management tool (this one, if I'm allowed to add a link), I have written my own "mini ASP" by having HTML pages with C# code inside and then processing them dynamically by converting the page into C#, compiling the code and then execute it.
Pay attention that this might populate the application domain since you cannot unload the dynamically loaded script code.
An excerpt from such a HTML file looks like:
<div id="title">
<img src="../_Shared/images/32x32/component_blue_view.png" />
<h1>Test case "[$=tc.Title$]" - Details</h1>
</div>
Here, the [$= and $] are the equivalents of <%= and %>.
In another project I did something similar with the Microsoft VBScript interpreter; instead of compiling the code to C#, I compile it as a VBScript and let it execute then by the VBScript engine of the Microsoft Scripting host.
To handle resources like images and CSS, you can simply ship your own, integrated web server. I successfully did this with several projects by including this CodePlex project.
来源:https://stackoverflow.com/questions/5568280/dynamic-web-pages-within-a-c-sharp-winforms-app