How to get a handler for a path

  • Thread starter Thread starter Marvin Landman
  • Start date Start date
M

Marvin Landman

Hi,

Server.Transfer and Execute only accept Pages but I would like to be
able to execute handlers of any type.

I am able to do that using the IHttpHandler and IHttpHandlerFactory
interfaces but I am unable to obtain a handler for any given path.

I could use HttpHandlersSection for classic mode although I still had to
find a match manually but integrated configuration is not exposed and by
default I can't even read applicationHost.config.

Please help me getting a handler for any given path.

Thanks.

Best regards,
Marvin
 
there are a couple issues. when you call server transfer, you are in the
Process method of the current running page handler. when server transfer
loads the next page, the handler knows it inside a running request, and
has code to support that state.

other handlers may not work. if they are your own, then you can load
them and call a special entry point.

-- bruce (sqlwork.com)
 
I understand that there are problems but also note that Server.Transfer
only works for System.Web.UI.Page instances not any handlers.

I actually would like to know if the given path is mapped to my handler
and if so I would execute it kind of a subrequest.

Thanks.

Marvin
 
Hi Marvin,
Please help me getting a handler for any given path.

Is my understanding correct that you want to have an HttpHandler to handle
all requests? If so you can config the varb and path as "*", like below:

<system.webServer>
...

<handlers>
...

<add name="mycustomhandler" verb="*" path="*"
type="YourHandlerClass" />

If it's not what you need could you clarify what you mean by "a handler for
any given path"?

Regards,
Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi,

Thank you for your reply.

I have a hanlder that can handle any path but I let my users chose what
files, extensions (or wildcard) they want to handle.

So I don't know what they have configured.

And I would like to be able to get the actually configured handler for
any given path/verb so that I can know if that's mine and handle specially.

Thanks.

Marvin
 
Hi Marvin,
Thank you for your reply.
I have a hanlder that can handle any path but I let my users chose what
files, extensions (or wildcard) they want to handle.
So I don't know what they have configured.
And I would like to be able to get the actually configured handler for
any given path/verb so that I can know if that's mine and handle specially.

I'm a little confused. Is my understanding correct that:

1. You have control over the HttpHandler class only.
2. You don't know how the users of HttpHandler set their web.config and CAS
of server machine.
3. With above restrictions, you still want your HttpHandler to handle all
requests.

Am I right? If so as far as I know there's no way to do so because it's
insecure. If my understanding is wrong could you please clarify what you
want to do?

BTW, have you tried HttpModule? Can it solve this issue?

http://msdn.microsoft.com/en-us/library/aa719858(VS.71).aspx

Regards,
Allen Chen
Microsoft Online Support
 
Allen Chen [MSFT] worte:
1. You have control over the HttpHandler class only.

I let my usesr decide what extension/files they handle with my
HttpHandler and as a result I can't know their settings.
2. You don't know how the users of HttpHandler set their web.config and CAS
of server machine.

I can demand full-trust if that's required but I don't want to require
Local System privileges to read applicationHost.config.
3. With above restrictions, you still want your HttpHandler to handle all
requests.

I don't want to handle all requests I just want to let my users decide
what requests they handle but later I need to know if a given path is
handled by my hander because my system has a sub-request like feature
that needs this information.
Am I right? If so as far as I know there's no way to do so because it's
insecure. If my understanding is wrong could you please clarify what you
want to do?

I don't want to modify anything I just would like to be able to get a
HttpHandler type for any given path in the ASP.NET application even in
integrated pipeline mode.
BTW, have you tried HttpModule? Can it solve this issue?

I believe that HttpModules are not relevant for this issue.

Thanks.

Marvin
 
Hi Marvin,
I don't want to handle all requests I just want to let my users decide
what requests they handle but later I need to know if a given path is
handled by my hander because my system has a sub-request like feature
that needs this information.

Thanks for your reply. Could you elaborate what your "sub-request like
feature" is? Why it needs to know whether your handler can handle a given
path? We probably can work out another approach to solve the underlying
problem if you could provide more detailed information.

BTW, is it possible to send a HTTP reqeust to that given path in your
"sub-request like feature"? By checking the HTTP response we can know
whether it's handled by your handler. (in your HttpHandler, you can add
specific HTTP header or return specific content in response to notify that
it's handled by your handler)

Regards,
Allen Chen
Microsoft Online Support
 
Allen said:
Hi Marvin,


Thanks for your reply. Could you elaborate what your "sub-request like
feature" is? Why it needs to know whether your handler can handle a given
path? We probably can work out another approach to solve the underlying
problem if you could provide more detailed information.

BTW, is it possible to send a HTTP reqeust to that given path in your
"sub-request like feature"? By checking the HTTP response we can know
whether it's handled by your handler. (in your HttpHandler, you can add
specific HTTP header or return specific content in response to notify that
it's handled by your handler)

Thank you for your reply.

I only need to know if a given path is handled by my handler and throw
an exception (or present an error) otherwise.

I can't use Server.Execute because that requires handlers derived from
System.Web.UI.Page, and even if I used a handler derived from Page,
Server.Execute would execute real Pages (.aspx) as well that I don't
want to.

I could (with some extra work) use system.web/httpHandlers but I have no
idea how to get a hanlder in integrated mode.

Marvin
 
Hi Marvin,
I only need to know if a given path is handled by my handler and throw
an exception (or present an error) otherwise.

Thanks for your clarification. Then how about this simple approach
mentioned in my previous reply:

Is it possible to send a HTTP reqeust to that given path in your
"sub-request like feature"? By checking the HTTP response we can know
whether it's handled by your handler. (in your HttpHandler, you can add
specific HTTP header or return specific content in response to notify that
it's handled by your handler)

You can use WebClient to request that path and check the response. Can it
solve this issue?

Regards,
Allen Chen
Microsoft Online Support
 
Hi,

Thank you for your reply.
Is it possible to send a HTTP reqeust to that given path in your
"sub-request like feature"? By checking the HTTP response we can know
whether it's handled by your handler. (in your HttpHandler, you can add
specific HTTP header or return specific content in response to notify that
it's handled by your handler)

You can use WebClient to request that path and check the response. Can it
solve this issue?

What I want to avoid is executing handlers other than mine. In fact I
don't have to execute my handler but even if I execute it that is not a
problem. By making a request I would execute any handler on that URL.

Marvin
 
Hi Marvin,
What I want to avoid is executing handlers other than mine. In fact I
don't have to execute my handler but even if I execute it that is not a
problem. By making a request I would execute any handler on that URL.

Thanks for your update. I believe we can move forward if you could clarify
this point:

You mentioned:
because my system has a sub-request like feature
that needs this information.

What is your "system"? Is it an ASP.NET application? Does it run on the web
server or another machine?

We probably can work out a solution if this point is clarified.

Regards,
Allen Chen
Microsoft Online Support
 
Allen said:
What is your "system"? Is it an ASP.NET application? Does it run on the web
server or another machine?

Thank your for your response.

This is a development framework based on the ASP.NET infrastructure and
intended to be used by web applications.

Marvin
 
Hi Marvin,
This is a development framework based on the ASP.NET infrastructure and
intended to be used by web applications.

Thanks for update. Do you mean you want to check the path in the same
application? If it is you probably can try the following approach if
reflection is an acceptable option:


protected void Page_Load(object sender, EventArgs e)
{
// Given path
string vp = "~/1.allenchen";
var virtualpath =
Assembly.GetAssembly(typeof(HttpApplication)).CreateInstance("System.Web.Vir
tualPath", false, BindingFlags.Instance | BindingFlags.NonPublic, null, new
object[] { vp }, CultureInfo.CurrentCulture, null);
// Use MapHttpHandler method for classic mode,
MapIntegratedHttpHandler for integrated mode
// If you test it in ASP.NET Web Development server it may
throw exception.
// Please publish it to IIS and use Integriated mode to test
MethodInfo mi =
HttpContext.Current.ApplicationInstance.GetType().GetMethod("MapIntegratedHt
tpHandler", BindingFlags.Instance | BindingFlags.NonPublic);

var o = mi.Invoke(HttpContext.Current.ApplicationInstance, new
object[] { HttpContext.Current, "GET", virtualpath,
Request.ServerVariables["PATH_TRANSLATED"], false,false });

if (!(o is MyHandler))
{
throw new Exception("Not handled by MyHandler");
}

}

Please test it to see if it can solve this issue.

Regards,
Allen Chen
Microsoft Online Support
 
Hi Marvin,
This is a development framework based on the ASP.NET infrastructure and
intended to be used by web applications.

Can my suggestion solve this issue?

Regards,
Allen Chen
Microsoft Online Support
 
Allen said:
Can my suggestion solve this issue?

I was busy lately but now I got some time for evaluating your
suggestion. Although I'm not happy that I have to rely on implementation
details the solution perfeclty fits my needs.

Thank you very much.

Regards,
Marvin
 
Back
Top