Entire page in assembly

Last post 07-30-2007 1:06 PM by DEHAAS. 6 replies.

Sort Posts:

  • Entire page in assembly

    07-03-2007, 9:40 AM
    • Member
      27 point Member
    • DEHAAS
    • Member since 06-04-2006, 12:08 PM
    • Posts 41

    Hi,

    I am currently working on a rather big project, and I am in great need of help for trying to use ASP.NET in a way it probably was not intended to, or at least not is a very common approach.I want to put an entire WebForm into an assembly (not pre-compiled). This means only having an instance of the Page Class without the markup (.aspx). I have been trying to do this before, but I abandoned the idea because I simply could not make it work. However, now I really need a solution.First step was determine when to return this specific page. In order to do this, I made a class which inherited from the System.Web.UI.PageHandlerFactory. Here I have developed a little piece of code that determines which hander to return according to the url, pretty simple really.Then I created a new class which inherited from the System.Web.UI.Page to be returned for this special page.So far everything works wonderfully, the special handler is returned when it have to. However, now all the problems surface. First of all, the handler renders ‘nothing’ – the rendered HTML is just empty. Then I tried adding a label-control to the page at the load-event. Then the handler returned <span>text</span>.All right I needed a header and a body, so I tried adding those at the Init event. System.Web.UI.HtmlControl.HtmlHeader and .HtmlBodyNow when I try to append a control like a Button is says that my form must contain the runat=”server” attribute. I have tried adding af HtmlForm control to the HtmlBody, but I get the same result.

    I really hope someone can help me in resolving this issue.

  • Re: Entire page in assembly

    07-07-2007, 10:08 AM
    • Member
      372 point Member
    • Bravo9
    • Member since 06-27-2007, 2:09 PM
    • Posts 71

    When you add a new page to a website you will notice the following line at the top of the .aspx file:

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="testPage.aspx.cs" Inherits="testPage" %> 

    What you need to do is move the underlying .cs file into your assembly (you will need to reference System.Web). I typically put my type (which inherits from Page) in its own namespace.  Once you compile the assembly you can then get rid of the "CodeFile" attribute change the .aspx file to look like this:

    <%@ Page Language="C#" AutoEventWireup="true" Inherits="myNamespace.testPage" %> 

     A downfall of this approach is that Visual Studio can't resolve the underlying .cs file when you select "View Code" .

  • Re: Entire page in assembly

    07-10-2007, 5:37 AM
    • Member
      27 point Member
    • DEHAAS
    • Member since 06-04-2006, 12:08 PM
    • Posts 41
    Thanks for your reply.I have been trying that, but it is not exactly what I am looking for. This approach gives me the ability to encapsulate all my code-behind in an assembly, but I still need the markup file (which is what I am trying to get rid of).
  • Re: Entire page in assembly

    07-10-2007, 9:12 AM
    • Contributor
      3,121 point Contributor
    • sujitm
    • Member since 05-23-2007, 9:01 AM
    • Pune
    • Posts 512

    Hi,

    If the page you are trying to generate is a static HTML page and it does not contain much advanced ASP.Net controls etc, may be you can use following approach. Create an HTML page and embed this page into the assembly where the HttpHandlers and all are residing so that you don't need to ship the HTML page along with the assembly. Use WebResources to capture the contents of the HTML file embedded in the assembly. Once you have all the HTML contents in the OnLoad() event of the Page class write the HTML contents in the response.

    I have never tried this but theoretically I don't see any problem with this approach. Let us know if it really worked. ;)

    - Sujit

    Dont forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: Entire page in assembly

    07-10-2007, 9:06 PM
    Answer
    • Participant
      1,431 point Participant
    • rstrahl
    • Member since 08-20-2003, 1:08 PM
    • Paia, Hawaii
    • Posts 277
    • ASPInsiders
      TrustedFriends-MVPs

    You can do this by creating a separate ASP.NET project (WAP or stock) and compiling it with Web Deployment projects into a single assembly. It works better with WAP projects because you'll get to set the class name rather than ASP.NET creating a dynamic name you'll have to figure out later with Reflector. Anyway once you've compiled with Web Deployment projects you have a single assembly that contains both the code behind and the compiled markup code.

    Once you have that you can create a custom HttpHandler that can invoke your custom class based on your processing rules. You simply override the ProcessRequest method in the handler and call your Page Class ProcessRequest() method instead.

    I wrote about this process here a while back:

    http://west-wind.com/WebLog/posts/8191.aspx

    As it turns out I've never actually put this to use though. I found it gets pretty tricky when you're dealing with related resources as you pretty much have to make sure all images and styles etc. are properly stowed into the assembly to ensure that the form is truly self contained. It's quite a bit of work of making this clean and easy but I suppose in some scenraios no extra resources may be required.

    +++ Rick --- 

    Rick Strahl
    West Wind Technologies
    Making waves on the Web
    www.west-wind.com/weblog
  • Re: Entire page in assembly

    07-17-2007, 12:38 AM
    • Member
      27 point Member
    • DEHAAS
    • Member since 06-04-2006, 12:08 PM
    • Posts 41

    Sorry about my late answer, but I am currently on vacation.

    This seems it like a great approach, I will try out as soon as possible. However, I see one problem. I would like make use of MasterPages. This should not be a problem for pages inside of this assembly, but what if I would like to create a new page (not in the assembly, but a standard .aspx page) inheriting from this MaterPage?

  • Re: Entire page in assembly

    07-30-2007, 1:06 PM
    • Member
      27 point Member
    • DEHAAS
    • Member since 06-04-2006, 12:08 PM
    • Posts 41
    I have now returned from my vacation.I have tried your approach, but I can't seem to get it working correctly. I do not get an error-message, but the page my handler returns is absolutely empty.Respectfully,

    Christopher de Haas

Page 1 of 1 (7 items)