Hi all, I must first apologising for posting this here as I know there is a dedicated HttpHandler/HttpModule forum here. I am getting a little desperate however... This httphandler intercepts requests for xml files and transforms them at the server end. What
I'm trying to do here is add a usercontrol to the output but I get the error "Request is not available in this context". Can anybody shed some light on the matter? Many thanks, Andy Sweetman. using System; using System.Configuration; using System.IO; using
System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Xml; using System.Xml.Xsl; using System.Xml.XPath; //using Solpart.WebControls; /// /// Summary description for XmlHandler. /// public class XmlHandler : System.Web.UI.Page, IHttpHandler
{ #region IHttpHandler Members new public void ProcessRequest(HttpContext context) { UserControl objMenu = (UserControl)LoadControl("/projects/process_sites/isdp/aspnet/usercontrols/banner.ascx"); HttpRequest Request = context.Request; HttpResponse Response
= context.Response; StreamWriter objStreamWriter = new StreamWriter(HttpContext.Current.Response.OutputStream); HtmlTextWriter objHtmlWriter = new HtmlTextWriter(objStreamWriter); Page.Controls.Add(objMenu); Page.Controls[0].RenderControl(objHtmlWriter); XPathDocument
doc = new XPathDocument(Request.PhysicalPath); XslTransform objTransform = new XslTransform(); objTransform.Load(ConfigurationSettings.AppSettings["WebStyleSheet"]); objTransform.Transform(doc, null, Response.OutputStream); } new public bool IsReusable { get
{ // TODO: Add XmlHandler.IsReusable getter implementation return true; } } #endregion }
Hey Andy, I've run this code with my user control, my XML and XSLT and everything worked ok. If your user controls isn't big what's it look like? I suspect the control, not the hanlder is causing trouble. Have you stepped through it in a debugger?
You do not need to implement IHttphandler if you are really just trying to load a page. You added "New" to ProcessRequest, which means your page's ProcessRequest method is not going to get called. This is what is probably causing your page to choke. Since XmlHandler
is really a page, you should use the page events (OnInit and OnLoad) and skip ProcessRequest. Try override protected void OnInit(EventArgs e) { UserControl objMenu = (UserControl)LoadControl("/projects/process_sites/isdp/aspnet/usercontrols/banner.ascx");
HttpRequest Request = context.Request; HttpResponse Response = context.Response; StreamWriter objStreamWriter = new StreamWriter(HttpContext.Current.Response.OutputStream); HtmlTextWriter objHtmlWriter = new HtmlTextWriter(objStreamWriter); Page.Controls.Add(objMenu);
Page.Controls[0].RenderControl(objHtmlWriter); XPathDocument doc = new XPathDocument(Request.PhysicalPath); XslTransform objTransform = new XslTransform(); objTransform.Load(ConfigurationSettings.AppSettings["WebStyleSheet"]); objTransform.Transform(doc, null,
Response.OutputStream); base.OnInit(e); } Also note, if you are really just transforming some XML, you can skip the page entirely and just use a HttpHandler. -Scott
Hi, I have a problem with very similar code. I'm using an httphandler to redirect all requests with .aspa extension to a class, which dynamically loads a control depending on the request path. Now this all works fine, until I try and put a <form runat="server">
tag into the control. I then get the error "A page can only have one server-side Form tag". If I remove it and view the source there are no form tags. I am definitely only loading this control once - so why is it complaining??? This is my class - PageHandler.vb
which handles the http requests Imports System.IO Imports Microsoft.ApplicationBlocks.Data Imports System.Data.SqlClient Public Class PageHandler Inherits System.Web.UI.Page Protected Overrides Sub OnInit(ByVal e As EventArgs) Dim objStreamWriter As
StreamWriter = New StreamWriter(HttpContext.Current.Response.OutputStream) Dim objHtmlWriter As HtmlTextWriter = New HtmlTextWriter(objStreamWriter) Dim ctrl As PageTemplate = LoadControl(GetControlPath(Request.FilePath)) Page.Controls.Add(ctrl) ctrl.PageId
= objTree.PageId ctrl.RenderControl(objHtmlWriter) End Sub Private Function GetControlPath(ByVal strRequestPath As String) As String 'code here which returns virtual path of user control to load Return "/controls/standardpage.ascx" End Function End Class
Any ideas???
More information..... I created a test aspx page and added the same control, and it works perfectly:
The "A page can only have one server-side Form tag" error only happens when I add the control to the page dynamically in the http handler code (which I included in the previous post). Does anyone have any ideas? I am stumped and my deadline is
getting closer and closer......... many thanks Rob
None
0 Points
11 Posts
Off topic? Httphandler problem...
Feb 18, 2004 07:46 AM|ADSweetman|LINK
Member
130 Points
293 Posts
Re: Off topic? Httphandler problem...
Feb 19, 2004 03:50 PM|MilanNegovan|LINK
http://www.AspNetResources.com
ASP.NET With Emphasis On Web Standards
Member
4 Points
718 Posts
ASPInsiders
Re: Off topic? Httphandler problem...
Feb 24, 2004 11:28 AM|ScottW|LINK
http://www.scottw.com
None
0 Points
21 Posts
Re: Off topic? Httphandler problem...
Jul 26, 2004 07:12 AM|rob1978|LINK
Imports System.IO Imports Microsoft.ApplicationBlocks.Data Imports System.Data.SqlClient Public Class PageHandler Inherits System.Web.UI.Page Protected Overrides Sub OnInit(ByVal e As EventArgs) Dim objStreamWriter As StreamWriter = New StreamWriter(HttpContext.Current.Response.OutputStream) Dim objHtmlWriter As HtmlTextWriter = New HtmlTextWriter(objStreamWriter) Dim ctrl As PageTemplate = LoadControl(GetControlPath(Request.FilePath)) Page.Controls.Add(ctrl) ctrl.PageId = objTree.PageId ctrl.RenderControl(objHtmlWriter) End Sub Private Function GetControlPath(ByVal strRequestPath As String) As String 'code here which returns virtual path of user control to load Return "/controls/standardpage.ascx" End Function End Class
Any ideas???None
0 Points
21 Posts
Re: Off topic? Httphandler problem...
Jul 26, 2004 10:31 AM|rob1978|LINK
The "A page can only have one server-side Form tag" error only happens when I add the control to the page dynamically in the http handler code (which I included in the previous post). Does anyone have any ideas? I am stumped and my deadline is getting closer and closer......... many thanks Rob