pre-process to ASP.NET...

  • Thread starter Thread starter Paul Colton
  • Start date Start date
P

Paul Colton

I want to pre-process some of my pages using some custom code, then output
the ASP.NET page so the regular ASP.NET handler takes care of it. I'd like
for page caching, etc. to work as normal.

What's the best approach?

-Paul
 
Hi,

I believe an HTTP module or custom code in global.asax would be what you are
looking for. In this article there are listed the HttpApplication events you
can wire handlers on (reusable HTTP module or app-specific code in
global.asax, both can get to the same events)
http://www.eggheadcafe.com/articles/20030211.asp

Here is an article about how to implement HTTP modules.
http://www.devx.com/vb2themax/Article/19901/0/page/1

--
Teemu Keiski
MCP, Microsoft MVP (ASP.NET), AspInsiders member
ASP.NET Forum Moderator, AspAlliance Columnist




I want to pre-process some of my pages using some custom code, then output
the ASP.NET page so the regular ASP.NET handler takes care of it. I'd like
for page caching, etc. to work as normal.

What's the best approach?

-Paul
 
I can write the HTTP module, but I can't figure out how to change the
filename, or content length, or content itself of the REQUEST, not of the
response. For example, what if I have a text file that contains macros that
I want to expand BEFORE ASP.NET processes the page?

Paul
 
Hi Paul,


Thanks for posting in the community!
From your description, you'd like to do some pre-process on a certain
ASP.NET web request?

In the former messages, Teemu Keiski has mentioned the means use the
"HttpModules". I thinkyou can also implement the same operations in the
HttpModules in the Application's Global object (Global.asax/
Global.asax.cs). There're also the same events in the Global object to help
you hook the web request's processing. For example:
protected void Application_BeginRequest(Object sender, EventArgs e)
{...}

protected void Application_EndRequest(Object sender, EventArgs e)
{...}

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{...}

Then, if you want to some operations on the Request's data, you can
retrieve the Request object via
"HttpContext.Current.Request" , it has several members to help do some
modifications on it, such as:
HttpContext.Current.Request.InputStream;
HttpContext.Current.Request.FilePath

For more detailed info on the Global object and Request object, you may
view the following reference in MSDN:
#Global.asax File
http://msdn.microsoft.com/library/en-us/cpguide/html/cpcontheglobalasaxfile.
asp?frame=true

#HttpRequest Class
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemWebHttpRequest
ClassTopic.asp?frame=true

In addition, since you mentioned that
" what if I have a text file that contains macros that
I want to expand BEFORE ASP.NET processes the page"

Do you mean that you want to do some modification on the certain ASPX
page's page source before it is compiled and used to process the Request?
If so, the aspx( httpHandler) will be created at runtime by HandlerFactory,
though it is described behind the HttpModules in the ASP.NET's pipline, but
it is likely that it has been compiled and created( the instance) when the
certain hooked events(Request_Begin...) is fired. So I think as for such
situation, maybe you can consider implement a custom httphandler which will
deal with the certain kinds of request, then delegate the other detailed
processing behind that httphandler( it is just like a proxy), how do you
think of this?

Here're some tech references and articles on HttpHandlers:
#HttpHandlers
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconhttphandlers.asp?f
rame=true

#Implementing Front Controller in ASP.NET Using HTTPHandler
http://msdn.microsoft.com/library/en-us/dnpatterns/html/ImpFrontControllerIn
ASP.asp?frame=true

And here are some other tech articles I've searched on the web:
#ASP.NET MVC Web-Tier
http://moncs.cs.mcgill.ca/people/hv/teaching/MSBDesign/codagen.pdf

Hope they're helpful.



Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Then, if you want to some operations on the Request's data, you can
retrieve the Request object via
"HttpContext.Current.Request" , it has several members to help do some
modifications on it, such as:
HttpContext.Current.Request.InputStream;
HttpContext.Current.Request.FilePath

One problem is that InputStream and FilePath are both read-only properties,
so I can't change their values.
In addition, since you mentioned that
" what if I have a text file that contains macros that
I want to expand BEFORE ASP.NET processes the page"

Do you mean that you want to do some modification on the certain ASPX
page's page source before it is compiled and used to process the Request?
If so, the aspx( httpHandler) will be created at runtime by HandlerFactory,
though it is described behind the HttpModules in the ASP.NET's pipline, but
it is likely that it has been compiled and created( the instance) when the
certain hooked events(Request_Begin...) is fired. So I think as for such
situation, maybe you can consider implement a custom httphandler which will
deal with the certain kinds of request, then delegate the other detailed
processing behind that httphandler( it is just like a proxy), how do you
think of this?

Can you explain this further? So let's say I want to pre-process the ASP.NET
source file before handing it over to asp.net, how would I 'delegate' my
updated file over to asp.net?
 
Hi Paul,

Thanks for your followup. I've done some further research on this issue and
here is some further infos maybe helpful to you:
1. As for how to modify the request content(stream) before it is processed
by the ASP.NET page handler, I think you can make use of the "Filter" for
the Request object. In ASP.NET both the Request and Response object has a
member named "filter" which is a instance of stream for class derieved from
stream, we can use "Filter" to do some certain modification on the Request
before it being processed or on the Response after it has been processed.
As for your situation, I think the Request filter is what you needed , and
you need to use it in a HttpModule or in the Global
object(global.asax/global.asax.cs) 's certain event such as BeginRequest...
For detailed info and example, you can refer to the following tech
articles:

#HttpRequest.Filter Property
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwebhttprequest
classfiltertopic.asp?frame=true

#Filtering HTTP Requests with .NET
http://www.ondotnet.com/pub/a/dotnet/2003/10/20/httpfilter.html

2. I also found that in ASP.NET it has provided the URL Rewriting function
which allow use to do some certain redirect actions before the certain
request has been processed. I think this will be helpful if you'd like to
modify the request url path. Here is also a tech article discussing this
topic:

#URL Rewriting with ASP.NET
http://www.codeproject.com/aspnet/URLRewriter.asp

Please check out the above items. If you still feel them not quite
suitable, please feel free to post here. I'll be willing to assit you on
further requests.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Filters can't alter the size of the request, but it looks like URL rewriting
may be my ticket. Thank you.

Paul
 
hi all,
if i undertand what paul is asking here, i want to do
the same, but somehow i don't believe it's possible.
let's put it in an ultra-simple example:
file a.aspx:
-------------------
<%@ PAGE LANGUAGE = C# %>
<html>
<body>
this is a line<br>
</body>
</html>
-------------------

is there any way a filter/modifier can be employed that will change
the content of the file a.aspx (and other aspx files)
just before it is handed over to asp.net processor.
for example the filter makes the following change on the
fly and then asp.net processes and renders the page:
(notice the added server-side include, which
may contain code blocks)
-------------------
<%@ PAGE LANGUAGE = C# %>
<html>
<body>
this is a line<br>
<!-- #Include virtual=".\include\temp.inc" -->
</body>
</html>
-------------------

i don't want to modify a.aspx as it exists on disk, i just want
the server-side include (or other line(s)) to be added on the fly,
before asp.net starts processing the file. why? i may have
thousands of aspx pages that i want this done to. i don't
want to modify and save each one. can it be done through a
hook or something similar? my feeling is that by the time
the file is loaded by iis and about to be routed to asp.net,
it's already too late to make changes to it. one may have to
write a lower level (file system level) hook to modify the file
content as it's being accessed by iis.

thanks,
rh
 
Hi rh,

As for this problem, Paul's question is infact dynamically redirect the
request to a certain handler at runtime. And it is possible to use
UrlRewrite to accomplish it. However, as for the modify aspx source at
runtime, it is not the approachs discussing in this thread. The ASP.NET
will compiled the page source and codebehind into dynamic assembly at
runtime, but we can't expect the time when it complete this, so modify the
source at httpmodule or other place won't work as we expect.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Back
Top