Search code examples
c#asp.netwebmethod

How to tell if an incoming request is a webmethod request?


In ASP.NET when a request comes in, how can I tell (prior to the actual webmethod gets called), if a request is a webmethod request?

Something along these lines:

    protected void Application_BeginRequest(object sender, EventArgs e)
    {

        if (IsWebMethod())
        {
            DoLogging();
        }
    }

What tests do I need to accurately determine when IsWebMethod() should be true?

Why: We're exposing several webservices (.asmx) and I'm trying to implement a DRY technique for capturing information about these calls.


Solution

  • Since a WebMethod call is just a regular GET or POST HTTP request, I don't think you can do anything to identify whether this is a WebMethod call or not besides inspecting the Request Url and determining if it matches one of the URLs for your WebMethods.

    In other words, assuming you web service is called default.asmx and has a method DoSomething you could inspect the request to see if it matches

    default.asmx/DoSomething 
    

    and then log it. I guess it's safe to assume that since your web methods will be exposed under default.asmx, you can just look for that string in the Request's URL.