4

Is it possible to take a page object, or maybe just a single ASP.NET control, and render it in a desktop application in some way?

For example, Visual Studio, being a desktop application renders ASP.NET controls in the design surface. I want to do something similar.

Edit: I am brainstorming on how to make a very simple designer for a very simple data entry application. I would have some custom ASP.NET data entry controls, and I want lay users to be able to see what the page might look like as close as possible. So I would want a panel in the application to show the rendered collection of controls, but the panel does not need to be interactive in any way. I.e. no click and drag or resizing of controls, etc. There will be standard windows form controls that the form author interacts with for defining the layout of the page.

I will simply save a list of the controls they added with some other information the "designer"(who are non-technical experts) provides, and I will use that information to later create the actual aspx page, either through a manual or automated process, TBD.

AaronLS
  • 37,329
  • 20
  • 143
  • 202
  • Visual Studio does not render ASP.Net controls the way you're trying to. (It doesn't execute any C# code in the controls) – SLaks Feb 16 '10 at 19:09
  • @Slaks Yeh, I know controls can provide a "faked" rendering just for design support, but I don't need the controls to behave like they would on a webpage, I just need them to look as close as possible to how they'd appear on a page. – AaronLS Feb 16 '10 at 20:22

5 Answers5

7

Yes, it is possible.

You'll need to create a separate AppDomain for ASP.Net, as described here.

EDIT: To render pages, use the following code:

string htmlSource;
var page = (Page)BuildManager.CreateInstanceFromVirtualPath(virtualPath, typeof(Page));

page.ProcessRequest(new HttpContext(new SimpleWorkerRequest("", null, null)));
using (var writer = new StringWriter(CultureInfo.InvariantCulture)) {
    using (var htmlWriter = new HtmlTextWriter(writer))
        page.RenderControl(htmlWriter);
    htmlSource = writer.ToString();
}

Note that this code must run in the ASP.Net AppDomain.
To do that, you could put in an a public method of the type you give to CreateApplicationHost.

Community
  • 1
  • 1
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    But that creates an ordinary EXE host, not an ASP host. There are different CLR hosts, and they behave differently. While the difference between the EXE and ASP hosts are not as big as between the EXE and SQL hosts (these being the 3 CLR hosts shipped by MS), the differences are exactly in the ASP specifics, like IIS request environemnt and HTTP request. I have never tried to host an ASP.Net compoenent in an EXE host, I'd be courious if it works. – Remus Rusanu Feb 16 '10 at 19:12
  • @Remus: I'm doing it in production in my application. It's not CLR host; it's simply a second AppDomain created by ASP.Net. – SLaks Feb 16 '10 at 19:26
  • I'll be damn... "Enables hosting of ASP.NET pages outside the Internet Information Services (IIS) application. This class enables the host to create application domains for processing ASP.NET requests." ... Wasn't aware this is possible, but sounds like it does work as advertised. How do you make the fake 'request' to the component hosted in this app domain, and how do you get back the response? – Remus Rusanu Feb 16 '10 at 19:29
  • @aaronls: No; this has nothing to do with IIS. – SLaks Feb 16 '10 at 20:53
3

I was not able to get the popular answer here to work. This, however, seems to work:

public string AspxToString(string aspx)
{
    StringBuilder sb = new StringBuilder();
    using (StringWriter sw = new StringWriter(sb))
    {
        using (HtmlTextWriter tw = new HtmlTextWriter(sw))
        {
            var workerRequest = new SimpleWorkerRequest(aspx, "", tw);
            HttpContext.Current = new HttpContext(workerRequest);

            object view = BuildManager.CreateInstanceFromVirtualPath(aspx, typeof(object));

            Page viewPage = view as Page;
            if (viewPage == null)
            {
                UserControl viewUserControl = view as UserControl;
                if (viewUserControl != null)
                {
                    viewPage = new Page();
                    viewPage.Controls.Add(viewUserControl);
                }
            }

            if (viewPage != null)
            {
                HttpContext.Current.Server.Execute(viewPage, tw, true);

                return sb.ToString();
            }

            throw new InvalidOperationException();
        }
    }
}

If you're running this from a desktop application, you'll need an AppDomain, see Is there a way to process an MVC view (aspx file) from a non-web application?

Community
  • 1
  • 1
marq
  • 808
  • 10
  • 13
1

I don't think that is possible but you can you the WebBroswer control load the asp.net webpage in it.

Shoban
  • 22,920
  • 8
  • 63
  • 107
0

Yes, it should be possible. To render it mean to get the html of the control, I don't sure how you gonna use that HTML in your application.

Fitzchak Yitzchaki
  • 9,095
  • 12
  • 56
  • 96
0

This article, "Using the ASP.Net Runtime for extending desktop applications with dynamic HTML Scripts", gives an example of how to run an embedded ASP.net runtime that goes beyond the one shown in the accepted answer.

mhvelplund
  • 2,099
  • 3
  • 22
  • 38