Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render ASP.NET controls in a desktop application?

Tags:

c#

.net

asp.net

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.

like image 906
AaronLS Avatar asked Dec 22 '22 05:12

AaronLS


2 Answers

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.

like image 119
SLaks Avatar answered Jan 05 '23 23:01

SLaks


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?

like image 43
marq Avatar answered Jan 05 '23 22:01

marq