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.
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:
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).
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.
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.
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.
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?
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,
DEHAAS
Member
27 Points
41 Posts
Entire page in assembly
Jul 03, 2007 01:40 PM|LINK
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.
Bravo9
Member
372 Points
71 Posts
Re: Entire page in assembly
Jul 07, 2007 02:08 PM|LINK
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" .
DEHAAS
Member
27 Points
41 Posts
Re: Entire page in assembly
Jul 10, 2007 09:37 AM|LINK
sujitm
Contributor
3153 Points
518 Posts
Re: Entire page in assembly
Jul 10, 2007 01:12 PM|LINK
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. ;)
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.
rstrahl
Contributor
2095 Points
366 Posts
ASPInsiders
MVP
Re: Entire page in assembly
Jul 11, 2007 01:06 AM|LINK
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 ---
West Wind Technologies
Making waves on the Web
www.west-wind.com/weblog
DEHAAS
Member
27 Points
41 Posts
Re: Entire page in assembly
Jul 17, 2007 04:38 AM|LINK
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?
DEHAAS
Member
27 Points
41 Posts
Re: Entire page in assembly
Jul 30, 2007 05:06 PM|LINK
Christopher de Haas